It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Need Help with Searching .txt file for specific record

Moderator: dafunkymunky

Need Help with Searching .txt file for specific record

Postby chineloogbonna on Mon Aug 01, 2011 8:07 pm

Hello...is there anyone out there that can help me. Basically I have an app that should be able to search a .txt file for a record by name or customer number. I've tried using both the "indexOf" and "contains" and still cannot get it to work??
chineloogbonna
 
Posts: 2
Joined: Mon Aug 01, 2011 8:00 pm

Re: Need Help with Searching .txt file for specific record

Postby chineloogbonna on Thu Aug 04, 2011 5:48 am

Hello again...Is there anyone who can help me with the below code. When a name is imputed into the (textSearch.text) and the name button is selected my app should search my CustomerData.txt file for that specific name and display only the data that pertains to that record in the listbox.

This is what I have so far, but it's not working and I am not sure what I've done wrong. Any help would be greatly appreciated! Thanks.

Imports System.IO
Public Class Form1

Public Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim CustomerAccounts As StreamWriter
Dim intAccountBalance As Integer



Try
If Integer.TryParse(txtAccountBalance.Text, intAccountBalance) Then


'open file
CustomerAccounts = File.AppendText("CustomerData.txt")

'Write sata to file
CustomerAccounts.WriteLine("Last Name: " & txtLastName.Text)
CustomerAccounts.WriteLine("First Name: " & txtFirstName.Text)
CustomerAccounts.WriteLine("Customer Number: " & txtCustomerNumber.Text)
CustomerAccounts.WriteLine("Address: " & txtAddress.Text)
CustomerAccounts.WriteLine("City: " & txtCity.Text)
CustomerAccounts.WriteLine("State: " & txtState.Text)
CustomerAccounts.WriteLine("Zip: " & txtZip.Text)
CustomerAccounts.WriteLine("Telephone Number: " & txtTelephoneNumber.Text)
CustomerAccounts.WriteLine("Account Balance: " & txtAccountBalance.Text)
CustomerAccounts.WriteLine("Date of Last Payment: " & txtLastPayment.Text)


'Close the file
CustomerAccounts.Close()

'Clear Text boxes
txtLastName.Clear()
txtFirstName.Clear()
txtCustomerNumber.Clear()
txtAddress.Clear()
txtCity.Clear()
txtState.Clear()
txtZip.Clear()
txtTelephoneNumber.Clear()
txtAccountBalance.Clear()
txtLastPayment.ResetText()

Else
MessageBox.Show("Please enter positive numeric number(s)")
End If
Catch
MessageBox.Show("Error: The file cannot be created.")
End Try

End Sub


Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clear Text boxes
txtLastName.Clear()
txtFirstName.Clear()
txtCustomerNumber.Clear()
txtAddress.Clear()
txtCity.Clear()
txtState.Clear()
txtZip.Clear()
txtTelephoneNumber.Clear()
txtAccountBalance.Clear()
txtLastPayment.ResetText()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Close the form
Me.Close()

End Sub


Sub SearchByName()
Dim inputFile As StreamReader

Dim strLastName As String = txtLastName.Text
Dim strFirstName As String = txtFirstName.Text
Dim strCustomerNumber As String = txtCustomerNumber.Text
Dim strAddress As String = txtAddress.Text
Dim strCity As String = txtCity.Text
Dim strState As String = txtState.Text
Dim strZip As String = txtZip.Text
Dim strTelephoneNumber As String = txtTelephoneNumber.Text
Dim strAccountBalance As String = txtAccountBalance.Text
Dim strFindSearch As String = txtSearch.Text

If File.Exists("CustomerData.txt") Then

'open the file
inputFile = File.OpenText("CustomerData.txt")

'Read the entire file into a string.
Do Until inputFile.Peek() <> -1
strLastName = inputFile.ReadLine()
strFirstName = inputFile.ReadLine()
strCustomerNumber = inputFile.ReadLine()
strAddress = inputFile.ReadLine()
strCity = inputFile.ReadLine()
strState = inputFile.ReadLine()
strZip = inputFile.ReadLine()
strTelephoneNumber = inputFile.ReadLine()
strAccountBalance = inputFile.ReadLine()
Loop

If strFindSearch = strLastName Then
'Display the data in the list box.
lstSearchResults.Items.Add(strLastName)
lstSearchResults.Items.Add(strFirstName)
lstSearchResults.Items.Add(strCustomerNumber)
lstSearchResults.Items.Add(strAddress)
lstSearchResults.Items.Add(strCity)
lstSearchResults.Items.Add(strState)
lstSearchResults.Items.Add(strZip)
lstSearchResults.Items.Add(strTelephoneNumber)
lstSearchResults.Items.Add(strAccountBalance)

Else
MessageBox.Show("Not Found")

End If


'Close the file
inputFile.Close()

Else
MessageBox.Show("Customer Data File Does Not Exitst.")
End If
End Sub

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
SearchByName()
End Sub
End Class
chineloogbonna
 
Posts: 2
Joined: Mon Aug 01, 2011 8:00 pm


Who is online

Users browsing this forum: No registered users and 3 guests