Solved

How to search for records in a text file?

Posted on 2004-09-26
4
159 Views
Last Modified: 2010-04-23
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

0
Comment
Question by:rkckjk
  • 2
4 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 12157530
I create a dataset of all the records...then apply a filter to the dataset by mapping a DATAVIEW to the dataset and then performing a filter on the DATAVIEW and then assigning the DATAVIEW to the Listbox....so basically

dsTotalList -----> Populate dataset based on sql query without filters (select * from tblBlah)

dim dvList as dataview

dvList.table = dsTotalList.Tables("tblBlah")

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.
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12157679
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(SearchText) > -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
0
 
LVL 2

Author Comment

by:rkckjk
ID: 12159526
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
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 125 total points
ID: 12166707
I just tested the code at it works fine for me,
If you can specify what error or behaviour its showing at your end, then perhaps I night be able to help.

I f I modify the code as per your requirements it should look like this

        Dim PhoneFile As Long
        PhoneFile = FreeFile
        Dim str As String
        Dim strng2 As String
        Dim x As Integer

        FileOpen(PhoneFile, "C:\Phonelist.txt", OpenMode.Input, OpenAccess.Read)
        While Not EOF(PhoneFile)
            Str = LineInput(PhoneFile)
            strng2 = Mid(str, 8, 80)
            x = InStr(1,strng2, txtSearchString, CompareMethod.Text)
            If x > 0 Then
                lstResults.Items.Add(strng2)
            End If
        End While
        FileClose(1)


I have done away with the Index of Method and used the Instr & Mid instead because I feel you are more comfortable with that.
Now if you compare the VB5 code and .Net code you'll see there's not much difference.
if you find any error while running this code post the details so that I can check and see.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net - How to check if current user is an administrator? 6 45
I need help making a collection from a class in VB.Net 3 32
Disable extension 8 37
Sql server insert 13 27
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question