How do I write a code to search for part of a specific record from random access file (e.g. LastName)
and then display the matched record on the form?
Printable View
How do I write a code to search for part of a specific record from random access file (e.g. LastName)
and then display the matched record on the form?
I would use a Sequential Access File, so the search can be controlled and use an EOF command, but This may or may not work. I didn't try it.
Lee
Don't forget to add the error trap, and close the file!Code:Dim strSearchName As String
Dim strName As String
Dim intCount As Integer
Dim Found As Boolean
strSearchName = txtName.Text
Intcount = 1 'Random Files Start At 1
Open "C:\MyFile.Dat" For Random As #1
On Error Resume Next
Do Until Found = True
Get #1, intCount, strName
If strName = strSearchName Then
Found = True
lblName.Caption = strName
End If
intCount = intCount + 1
Loop
'Code improved by vBulletin Tool (Save as...)
I'm not sure if this is what you're looking for, but when I search through Random Access Files I do this:
If it doesn't work or you don't understand what I've written (I'm the first to admit I'm no good at explaining) let me know and I'll try to give you a better exampleCode:Private Sub FindRecord_Click()
Dim Count As Integer
Open "c:\Myfile.dat" for Random As #1 Len=Len _ (RecordStructureName)
NoOfRecords=LOF(1)\Len(RecordStructureName)
SFLastName=InputBox("Enter Last Name")
Count=0
Found=False
While (Count<=NoOfRecords) And (Found=False)
Count=Count+1
Get #1, Count, RecordStructureName
If Trim(LastName.text)=SFLastName Then
Found=True
End If
Wend
If Found=True Then
LastName.text=RecordStructureName.LastName
'This transfers the fileds from the record stucture into text boxes
'Do this for all fields
End If
If Found=False Then
MsgBox ("Record not found in file")
Close #1
End If
End If
End Sub
Pix :)
Thanks,
I'll try to see if it works and let you know later.