Richard Kreidl
asked on
How to search for records in a text file?
Let's say I have the following text file that maybe has over 500 records in it:
Baker Sam 234-231-4534
Johnson Tom 312-454-2344
Johnson Larry 217-342-7882
King Steve 213-453-2133
Lewis Betty 321-433-4523
Money Tony 432-232-1243
and so on.....
I did this in VB 5.0 , but now I trying to convert my application to VB .Net. So, I guess I'm a newbie to VB .Net.
Basically, the way it worked in my VB 5.0 application was that a had a listbox that displayed all the records that it matched on from a user entering a name to search for in a textbox. The user would then hit a command button and open the text file and do a string search on the file. Example, if you just typed in 'John' it would display the following:
Johnson Tom 312-454-2344
Johnson Larry 217-342-7882
Thanks
Baker Sam 234-231-4534
Johnson Tom 312-454-2344
Johnson Larry 217-342-7882
King Steve 213-453-2133
Lewis Betty 321-433-4523
Money Tony 432-232-1243
and so on.....
I did this in VB 5.0 , but now I trying to convert my application to VB .Net. So, I guess I'm a newbie to VB .Net.
Basically, the way it worked in my VB 5.0 application was that a had a listbox that displayed all the records that it matched on from a user entering a name to search for in a textbox. The user would then hit a command button and open the text file and do a string search on the file. Example, if you just typed in 'John' it would display the following:
Johnson Tom 312-454-2344
Johnson Larry 217-342-7882
Thanks
Well I think there's not much difference in approach to the problem in VB.NET
This is how you can achieve it.
Dim TextLine As String
Dim SearchText As String = "John"
FileOpen(1, "C:\Test.Txt", OpenMode.Input)
While Not EOF(1)
TextLine = LineInput(1)
If TextLine.IndexOf(SearchTex t) > -1 Then
MsgBox(TextLine)
End If
End While
FileClose(1)
However if the user searches more often you can improve upon the performance by storing individual lines in an array once and then searching everytime on that array
This is how you can achieve it.
Dim TextLine As String
Dim SearchText As String = "John"
FileOpen(1, "C:\Test.Txt", OpenMode.Input)
While Not EOF(1)
TextLine = LineInput(1)
If TextLine.IndexOf(SearchTex
MsgBox(TextLine)
End If
End While
FileClose(1)
However if the user searches more often you can improve upon the performance by storing individual lines in an array once and then searching everytime on that array
ASKER
Arif eqbal,
I tried using your code above and nothing seems to work. Here is my code that I use iin the VB 5.0 application:
Dim PhoneFile As Long
PhoneFile = FreeFile
Dim str As String
Dim strng2 As String
Dim x As Integer
Open "C:\Phonelist.txt" For Input Access Read As PhoneFile
Do Until EOF(PhoneFile)
Screen.MousePointer = vbHourglass
Line Input #PhoneFile, str
strng2 = Mid$(str, 8, 80)
x = InStr(1, strng2, txtSearchString, vbTextCompare)
If x > 0 Then
lstResults.AddItem strng2
End If
Loop
Close #PhoneFile
I tried using your code above and nothing seems to work. Here is my code that I use iin the VB 5.0 application:
Dim PhoneFile As Long
PhoneFile = FreeFile
Dim str As String
Dim strng2 As String
Dim x As Integer
Open "C:\Phonelist.txt" For Input Access Read As PhoneFile
Do Until EOF(PhoneFile)
Screen.MousePointer = vbHourglass
Line Input #PhoneFile, str
strng2 = Mid$(str, 8, 80)
x = InStr(1, strng2, txtSearchString, vbTextCompare)
If x > 0 Then
lstResults.AddItem strng2
End If
Loop
Close #PhoneFile
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dsTotalList -----> Populate dataset based on sql query without filters (select * from tblBlah)
dim dvList as dataview
dvList.table = dsTotalList.Tables("tblBla
dvList.RowFilter = [varLongSupplierName] Like '" & txtUserFilter.Text & "'"
Map the view onto your Listbox.....
You can keep filtering and re-filtering the view every time the user hits the Command button...just change the RowFilter, then remap to the listbox...
I use this for filtering datagrids down as the user types in information...allowing them to use % for wild card since the RowFilter is similar to a SQL Where statement.