11 Replies - 3501 Views - Last Post: 20 April 2010 - 05:04 PM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Searching for a record in a text file

Posted 19 April 2010 - 07:03 PM

I need to search for a record by an employee number that is in a text file along with other records. I should be able to enter the number in a text box and click the search button and the record should be displayed on my form. Cannot get that done. Help me please

This is my code
Imports System.IO

Public Class frmViewRecord

    Dim ReadRecord As StreamReader

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
        ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        ofdOpenFile.Title = "Selct a File to Open"
        If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
            If ofdOpenFile.Filter <> String.Empty Then
                ReadFile(ofdOpenFile.FileName)
            Else
                MessageBox.Show("No file selected", "Error")
            End If
        End If
    End Sub

    Sub ReadFile(ByVal strFileName As String)
        ' Open the file
        ReadRecord = File.OpenText(strFileName)

        lblFirstName.Text = ReadRecord.ReadLine
        lblMiddleName.Text = ReadRecord.ReadLine
        lblLastName.Text = ReadRecord.ReadLine
        lblEmployeeNumber.Text = ReadRecord.ReadLine
        lblDepartment.Text = ReadRecord.ReadLine
        lblTelephone.Text = ReadRecord.ReadLine
        lblExtension.Text = ReadRecord.ReadLine
        lblEmail.Text = ReadRecord.ReadLine
    End Sub

    Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click

        If Not ReadRecord.Peek = -1 Then
            lblFirstName.Text = ReadRecord.ReadLine
            lblMiddleName.Text = ReadRecord.ReadLine
            lblLastName.Text = ReadRecord.ReadLine
            lblEmployeeNumber.Text = ReadRecord.ReadLine
            lblDepartment.Text = ReadRecord.ReadLine
            lblTelephone.Text = ReadRecord.ReadLine
            lblExtension.Text = ReadRecord.ReadLine
            lblEmail.Text = ReadRecord.ReadLine
        Else
            MessageBox.Show("End of Records.", "Notification")
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Clear()
    End Sub

    Sub Clear()
        txtEmpRecord.Text = String.Empty
        lblFirstName.Text = String.Empty
        lblMiddleName.Text = String.Empty
        lblLastName.Text = String.Empty
        lblEmployeeNumber.Text = String.Empty
        lblDepartment.Text = String.Empty
        lblTelephone.Text = String.Empty
        lblExtension.Text = String.Empty
        lblEmail.Text = String.Empty
    End Sub

    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
        pdPrint.Print()
    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

        e.Graphics.DrawString("Employee Data", New Font("Courier New", 16, FontStyle.Bold), Brushes.Black, 300, 20)

        e.Graphics.DrawString("First Name: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 100)
        e.Graphics.DrawString("Middle Name: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 150)
        e.Graphics.DrawString("Last Name: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 200)
        e.Graphics.DrawString("Employee Number: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 250)
        e.Graphics.DrawString("Department: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 300)
        e.Graphics.DrawString("Telephone: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 350)
        e.Graphics.DrawString("Extension: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 400)
        e.Graphics.DrawString("Email Address: ", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 20, 450)

        e.Graphics.DrawString(lblFirstName.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 100)
        e.Graphics.DrawString(lblMiddleName.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 150)
        e.Graphics.DrawString(lblLastName.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 200)
        e.Graphics.DrawString(lblEmployeeNumber.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 250)
        e.Graphics.DrawString(lblDepartment.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 300)
        e.Graphics.DrawString(lblTelephone.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 350)
        e.Graphics.DrawString(lblExtension.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 400)
        e.Graphics.DrawString(lblEmail.Text, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 250, 450)

        e.Graphics.DrawString(Now.ToString(), New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 10, 1000)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'Dim found As Integer = 4
        'Do Until (ReadRecord.Peek = -1) 
        If txtEmpRecord.Text =  Then
            SearchRecord()
        End If

        'Loop
    End Sub

    Private Sub SearchRecord()
        Dim intReadFile As Integer
        lblFirstName.Text = ReadRecord.ReadLine
        lblMiddleName.Text = ReadRecord.ReadLine
        lblLastName.Text = ReadRecord.ReadLine
        lblEmployeeNumber.Text = ReadRecord.ReadLine
        lblDepartment.Text = ReadRecord.ReadLine
        lblTelephone.Text = ReadRecord.ReadLine
        lblExtension.Text = ReadRecord.ReadLine
        lblEmail.Text = ReadRecord.ReadLine
        intReadFile = +1
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Searching for a record in a text file

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 179
  • View blog
  • Posts: 655
  • Joined: 03-February 10

Re: Searching for a record in a text file

Posted 20 April 2010 - 08:43 AM

First, there are plenty of tutorials and snippets on how to do this exact process, some even in DIC, otherwise a good search will come up with many results.

Second, you don't specify how the contents of the text file are composed. What is the delimiter?

Third, I would suggest reading the text file into something that can be searched more easily (Dictionary object, DataTable, DataSet).
Was This Post Helpful? 0
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 11:56 AM

View Postdemausdauth, on 20 April 2010 - 09:43 AM, said:

First, there are plenty of tutorials and snippets on how to do this exact process, some even in DIC, otherwise a good search will come up with many results.

Second, you don't specify how the contents of the text file are composed. What is the delimiter?

Third, I would suggest reading the text file into something that can be searched more easily (Dictionary object, DataTable, DataSet).


I waited and waited for someone to help me out with this last part of the program....and then all I get is "there are plenty of tutorials and snippets"....thank you for the your "valuable help"
Was This Post Helpful? 0
  • +
  • -

#4 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Searching for a record in a text file

Posted 20 April 2010 - 12:43 PM

Could you please answer the second question, about how your text file is delimited? You should be able to simply trace through the text file, checking each "record" for that employee number.

From your code, it appears that each line holds a different piece of employee information. Your SearchRecord function should maybe pass in the employee number that you are searching for, and then loop through all of the records in the file until you find a match.

demausdauth is correct, though, in that a DataSet or similar structure would be much easier to search. Just simply read the text in to that in a similar fashion. Then, you'd only have to read it once, not multiple times as your search function seems to be doing.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 12:55 PM

View Postcode_junkie83, on 20 April 2010 - 01:43 PM, said:

Could you please answer the second question, about how your text file is delimited? You should be able to simply trace through the text file, checking each "record" for that employee number.

From your code, it appears that each line holds a different piece of employee information. Your SearchRecord function should maybe pass in the employee number that you are searching for, and then loop through all of the records in the file until you find a match.

demausdauth is correct, though, in that a DataSet or similar structure would be much easier to search. Just simply read the text in to that in a similar fashion. Then, you'd only have to read it once, not multiple times as your search function seems to be doing.

Hope this helps!


The data in the text file is written vertically with no delimiter...Yes there are different pieces of information but they all considered data of type string. I modified this part of the program as follows:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        'Do Until ReadRecord.Peek = -1
        'ReadRecord = File.OpenText(ofdOpenFile.FileName)
        SearchRecord()
        If txtEmpRecord.Text = ReadRecord.ReadLine() Then
            ReadingFile()
        End If
        'Loop
    End Sub

    Sub SearchRecord()
        'Dim intReadFile As Integer
        'Do Until ReadRecord.Peek = -1
        ReadRecord = File.OpenText(ofdOpenFile.FileName)
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        ReadRecord.ReadLine()
        'intReadFile = +1
        'Loop
    End Sub

        Sub ReadingFile()
        lblFirstName.Text = ReadRecord.ReadLine
        lblMiddleName.Text = ReadRecord.ReadLine
        lblLastName.Text = ReadRecord.ReadLine
        lblEmployeeNumber.Text = ReadRecord.ReadLine
        lblDepartment.Text = ReadRecord.ReadLine
        lblTelephone.Text = ReadRecord.ReadLine
        lblExtension.Text = ReadRecord.ReadLine
        lblEmail.Text = ReadRecord.ReadLine
    End Sub


This post has been edited by asdbabil: 20 April 2010 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:11 PM

Did that solve your problem?
Was This Post Helpful? 0
  • +
  • -

#7 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:14 PM

View Postcode_junkie83, on 20 April 2010 - 02:11 PM, said:

Did that solve your problem?

Unfortunately, it didn't :(
Was This Post Helpful? 0
  • +
  • -

#8 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:22 PM

okay, I'm at work so I can't give it a ton of thought, but I believe your best bet may be to do the following:

1. Create a class that has the properties you need (first name, last name, employee number, etc)
2. Read in the information into an array/list of those objects
3. Using a for-each loop, navigate through the list/array until you find the one that matches your employee number.

I think that is going to be your best bet here. This accomplishes two things: it only reads in the data once, and stores it in a collection, and also this will be much more efficient and trustworthy at finding the data.
Was This Post Helpful? 1
  • +
  • -

#9 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:27 PM

View Postcode_junkie83, on 20 April 2010 - 02:22 PM, said:

okay, I'm at work so I can't give it a ton of thought, but I believe your best bet may be to do the following:

1. Create a class that has the properties you need (first name, last name, employee number, etc)
2. Read in the information into an array/list of those objects
3. Using a for-each loop, navigate through the list/array until you find the one that matches your employee number.

I think that is going to be your best bet here. This accomplishes two things: it only reads in the data once, and stores it in a collection, and also this will be much more efficient and trustworthy at finding the data.


Ok thank you for suggestions..I'll try that and re-post the final code :)
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:28 PM

OK, this should work for what you're wanting to do but you will need to continue it to fit your program.

   Dim ReadRecord As StreamReader
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim fn As String
        Dim mi As String
        Dim ln As String
        Dim en As String
        Dim dp As String
        Dim tn As String
        Dim ex As String
        Dim em As String
        ReadRecord = File.OpenText("D:\Employee.txt")
        Do Until ReadRecord.Peek = -1
            'ReadRecord = File.OpenText(ofdOpenFile.FileName)
            fn = ReadRecord.ReadLine
            mi = ReadRecord.ReadLine
            ln = ReadRecord.ReadLine
            en = ReadRecord.ReadLine
            dp = ReadRecord.ReadLine
            tn = ReadRecord.ReadLine
            ex = ReadRecord.ReadLine
            em = ReadRecord.ReadLine
            If en = TextBox1.Text Then
                MessageBox.Show("Employee # " & en & " Is for " & fn)
            End If
        Loop
    End Sub

It is based on the table format you gave, I put them in variables for quicker coding but you could just in the If statement use lblFirstName.Text = fn or even just put them in the labels instead of the variables.

you will also want an else condition in case there are no matches

Edit: Also want to note, since I posted this the same time code_junkie did, that I agree with his recommendation. This code is only demonstrating how to do what your code was trying to do.

This post has been edited by CharlieMay: 20 April 2010 - 01:41 PM

Was This Post Helpful? 1
  • +
  • -

#11 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 01:37 PM

CharlieMay,

Thank you for this code. I'm on my way to school now...I'll try this when I get there. :)

This post has been edited by asdbabil: 20 April 2010 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#12 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Searching for a record in a text file

Posted 20 April 2010 - 05:04 PM

Hello again,
I knew there was an easier way :)..It's working just the way I want it to work :bananaman:

Here's my code:
code...
code....
more code....
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim found As Boolean = False

        ' Close the file
        ReadRecord.Close()

        ' Re-open the file
        ReadRecord = File.OpenText(strFilename)


        Do Until ReadRecord.Peek = -1 Or found
            ' Call the ReadingFile()
            ReadingFile()

            If lblEmployeeNumber.Text = txtEmpRecord.Text Then
                found = True
            End If
        Loop

    End Sub

    ' Read all records in the text file and display them on the appropriate labels
    Sub ReadingFile()
        lblFirstName.Text = ReadRecord.ReadLine
        lblMiddleName.Text = ReadRecord.ReadLine
        lblLastName.Text = ReadRecord.ReadLine
        lblEmployeeNumber.Text = ReadRecord.ReadLine
        lblDepartment.Text = ReadRecord.ReadLine
        lblTelephone.Text = ReadRecord.ReadLine
        lblExtension.Text = ReadRecord.ReadLine
        lblEmail.Text = ReadRecord.ReadLine
    End Sub

code....
code....


This post has been edited by asdbabil: 20 April 2010 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1