|
-
Nov 27th, 2003, 03:21 AM
#1
Thread Starter
New Member
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:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim input As String 'What I'm searching for
Dim friendFile As System.IO.StreamReader
friendFile = System.IO.File.OpenText("database.txt")
'Use an input box to gain the search string
input = InputBox("Enter the last name of the data you'd like to see.", "Enter Last Name")
'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
Do
If friendFile.ReadLine = input.ToUpper Then
txtLastName.Text = input.ToUpper
txtFirstName.Text = friendFile.ReadLine()
txtCustNumber.Text = friendFile.ReadLine()
Else : MessageBox.Show("Customer not found.", "Error")
End If
Loop Until friendFile.ReadLine() <> input.ToUpper
End Sub
Last edited by mccullom; Nov 27th, 2003 at 03:49 AM.
-
Nov 28th, 2003, 09:24 AM
#2
Addicted Member
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]
-
Nov 28th, 2003, 01:27 PM
#3
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
-
Nov 28th, 2003, 04:08 PM
#4
Thread Starter
New Member
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:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim input As String
Dim friendFile As System.IO.StreamReader
friendFile = System.IO.File.OpenText("database.txt")
input = InputBox("Enter the last name of the data you'd like to see.", "Enter Last Name")
Do Until friendFile.Peek = -1
If friendFile.ReadLine = input.ToUpper Then
txtLastName.Text = input.ToUpper 'Display input in first text box
txtFirstName.Text = friendFile.ReadLine() 'Display first name of customer in second text box
txtCustNumber.Text = friendFile.ReadLine() 'Display customer number in third text box
Else : MessageBox.Show("Customer not found.", "Error")
End If
Loop
End Sub
-
Nov 28th, 2003, 10:23 PM
#5
ok from what I see the file looks like this:
Code:
LastName
FirstName
CustomerNumber
LastName
FirstName
CustomerNumber
...
This should do the trick:
VB Code:
Public Structure Customer
Dim LastName As String
Dim FirstName As String
Dim CustomerNumber As String
End Structure
Public Function FindCustomer(ByRef CustomerInfo As Customer) As Boolean
Dim sr As New System.IO.StreamReader("Database.txt")
Dim strFile() As String = Split(sr.ReadToEnd, vbCrLf)
Dim I As Integer
For I = 0 To strFile.GetUpperBound(0) Step 3 ' Stepping 3 always makes sure we land on a last name
If strFile(I).ToUpper = CustomerInfo.LastName.ToUpper Then
CustomerInfo.FirstName = strFile(I + 1)
CustomerInfo.CustomerNumber = strFile(I + 2)
'we found what we were looking for now lets not waste any time :D
Return True
End If
Next
End Function
now to use this:
VB Code:
'Put this in ur btnSearch's Click event
Dim CustomerToFind As Customer
CustomerToFind.LastName = InputBox("Please Type Customer Name: ", "Find Customer")
If FindCustomer(CustomerToFind) = True Then
'We Found a Customer
MsgBox(CustomerToFind.LastName & ", " & CustomerToFind.FirstName & " was Found!" & vbCrLf & _
"Customer Number: " & CustomerToFind.CustomerNumber, MsgBoxStyle.Information, "Customer Found!")
Else
MsgBox("Customer not Found!")
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
-
Nov 28th, 2003, 10:48 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|