Utilizzare il file di testo per fornire il valore predefinito per un campo nel modulo di accesso
-
02-01-2020 - |
Domanda
Ho una finestra pop-up in Microsoft Access contenente campi caselle di testo che devono essere compilati dall'utente, ad esempio:
First Name:
Last Name:
.
Ora sto provando a creare un pulsante che quando cliccava esaminare C: \ mytextfile.txt e popolare automaticamente quei campi.
Dentro il file di testo sembrerebbe questo:
##$@#%#$543%#$%#$$#%LAST NAME:BOB#$#@$@#$@#$@#FIRST NAME:DERRICK$#%$#%$#%#$%$#%$#
.
Quindi essenzialmente sto cercando 3 cose:
- .
- per accedere al file di testo
- per analizzare per i dati
- per popolarlo nelle caselle di testo.(I dati non devono andare in una tabella fino a quando non si fa clic sul pulsante "Salva" ")
Aggiornamento: Questo è quello che ho scritto finora, non sono sicuro del motivo per cui non funziona.
Private Sub LoadText_Click()
Dim myFile As String myFile = "C:\myFile.txt"
Me.NameofTextbox = Mid(myFile, 7, 3)
End Sub
.Soluzione
Qui Esempio per il file che hai fornito e controlli sul modulo denominato txtboxLastName
e txtboxFirstName
Dim mFields() As String ' array with fields' names in file
Dim mControls() As String ' corresponding controls' names
Dim mStopChars() As String ' Characters that put after values
Dim tmpstr As String
Dim content As String
Dim i As Long
Dim fStart As Long
Dim valStart As Long
Dim valEnd As Long
Dim FieldValue As String
Dim j As Long
Dim tmp As Long
' prepare maps
' here : included in field name for common case
mFields = Split("LAST NAME:,FIRST NAME:", ",")
mControls = Split("txtboxLastName,txtboxFirstName", ",")
mStopChars = Split("#,$,@,%", ",")
' read file into string
Open "c:\mytextfile.txt" For Input As #1
Do While Not EOF(1)
Input #1, tmpstr
content = content & tmpstr
Loop
Close #1
' cycle through fields and put their values into controls
For i = LBound(mFields) To UBound(mFields)
fStart = InStr(1, content, mFields(i))
If fStart > 0 Then
valStart = fStart + Len(mFields(i)) 'value start at this pos
'cycle through possible stop chars to locate end of current value
valEnd = Len(content)
For j = LBound(mStopChars) To UBound(mStopChars)
tmp = InStr(valStart, content, mStopChars(j))
If tmp > 0 Then
If tmp <= valEnd Then
valEnd = tmp - 1
End If
End If
Next j
' cut value
FieldValue = Mid(content, valStart, valEnd - valStart + 1)
' assign to control
Me.Controls(mControls(i)).Value = FieldValue
End If
Next i
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow