Results 1 to 6 of 6

Thread: Searching for text in a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    13

    Searching for text in a text file

    Hi, this will probably be simple for you guys but, I'm trying to search a text file for a last name, and then display the corresponding info underneath the name from the text file such as phone number, address, etc...After it finds it, it then displays it neatly in a windows form. I can't get it to work though..this is what I have so far.


    VB Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.         Dim input As String 'What I'm searching for
    3.         Dim friendFile As System.IO.StreamReader
    4.         friendFile = System.IO.File.OpenText("database.txt")
    5.         'Use an input box to gain the search string
    6.         input = InputBox("Enter the last name of the data you'd like to see.", "Enter Last Name")
    7.        
    8.         'This will only return true if I search for the first name in the text file, but I want it to be able to go down the entire text file
    9. Do            
    10. If friendFile.ReadLine = input.ToUpper Then
    11.                 txtLastName.Text = input.ToUpper
    12.                 txtFirstName.Text = friendFile.ReadLine()
    13.                 txtCustNumber.Text = friendFile.ReadLine()
    14.             Else : MessageBox.Show("Customer not found.", "Error")
    15.             End If
    16.     Loop Until friendFile.ReadLine() <> input.ToUpper
    17.     End Sub
    Last edited by mccullom; Nov 27th, 2003 at 03:49 AM.

  2. #2
    Addicted Member
    Join Date
    Nov 2003
    Location
    India
    Posts
    227
    Hi,

    This will become an unended job if your file is not formatted properly. I think you are going to write the file right. While writing in the file write it with specific format. Then read the file in the same format, loop thru it once you find your name, display the coresponding values.

    -Jai
    See you,
    -Jai
    [Friends Never Say Good Bye]

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    can you post a chunk of the file(doesnt need to contant sensitive data just the proper formatting)

    Did you make this file? If so use a Database (access or SQL) or XML
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    13

    Ok, I got a little further

    OK, I got it to find and display the records from the text file that I want, however, I can't get the error box to display correctly if the user inputs the last name of a customer who doesn't exist.

    The problem is that once it finds the record from the text file, it then continues to loop through until the end of the text file, while displaying an error message for every line that doesn't match before and after the correct record. How do I stop the loop once it has found the customer I want? Thanks for any help you may have.

    VB Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.         Dim input As String
    3.         Dim friendFile As System.IO.StreamReader
    4.         friendFile = System.IO.File.OpenText("database.txt")
    5.         input = InputBox("Enter the last name of the data you'd like to see.", "Enter Last Name")
    6.         Do Until friendFile.Peek = -1
    7.             If friendFile.ReadLine = input.ToUpper Then
    8.                 txtLastName.Text = input.ToUpper 'Display input in first text box
    9.                 txtFirstName.Text = friendFile.ReadLine() 'Display first name of customer in second text box
    10.                 txtCustNumber.Text = friendFile.ReadLine() 'Display customer number in third text box
    11.             Else : MessageBox.Show("Customer not found.", "Error")
    12.             End If
    13.         Loop
    14.     End Sub

  5. #5
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    ok from what I see the file looks like this:

    Code:
    LastName
    FirstName
    CustomerNumber
    LastName
    FirstName
    CustomerNumber
    ...
    This should do the trick:

    VB Code:
    1. Public Structure Customer
    2.         Dim LastName As String
    3.         Dim FirstName As String
    4.         Dim CustomerNumber As String
    5.     End Structure
    6.  
    7.     Public Function FindCustomer(ByRef CustomerInfo As Customer) As Boolean
    8.         Dim sr As New System.IO.StreamReader("Database.txt")
    9.  
    10.         Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
    11.  
    12.         Dim I As Integer
    13.  
    14.         For I = 0 To strFile.GetUpperBound(0) Step 3 ' Stepping 3 always makes sure we land on a last name
    15.             If strFile(I).ToUpper = CustomerInfo.LastName.ToUpper Then
    16.  
    17.                 CustomerInfo.FirstName = strFile(I + 1)
    18.                 CustomerInfo.CustomerNumber = strFile(I + 2)
    19.                 'we found what we were looking for now lets not waste any time :D
    20.                 Return True
    21.             End If
    22.  
    23.         Next
    24.  
    25.     End Function

    now to use this:

    VB Code:
    1. 'Put this in ur btnSearch's Click event
    2.         Dim CustomerToFind As Customer
    3.  
    4.         CustomerToFind.LastName = InputBox("Please Type Customer Name: ", "Find Customer")
    5.  
    6.         If FindCustomer(CustomerToFind) = True Then
    7.             'We Found a Customer
    8.             MsgBox(CustomerToFind.LastName & ", " & CustomerToFind.FirstName & " was Found!" & vbCrLf & _
    9.                 "Customer Number: " & CustomerToFind.CustomerNumber, MsgBoxStyle.Information, "Customer Found!")
    10.         Else
    11.             MsgBox("Customer not Found!")
    12.         End If
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    13

    Awesome man

    Awesome, it worked great!!!!!!! Now maybe I can watch football on sunday in peace...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width