Right guys, I'm really new to this but I'm getting along ok, but I've hit a block.

I need to be able to know how to search a text file in VB for a particular line and then display it.
the files I'm trying to use are set up as: ID, Name, Address, Birthday, Pay-Roll Type
So I need to know how to search the file for the ID number and then display the rest of that line

I can read a file from beginning to end but this I am clueless about, thanks
It's for a system to add, display, update and remove workers

Recommended Answers

All 4 Replies

Easy...just test the values immediately after you do each read. Stop reading when you find what you're looking for. Here's a code snippet to get you started:

Dim intId As Integer
Dim strname As String
Dim strAddress As String
Dim strBirthDate As String
Dim strPayrollType As String

Open App.Path & "\FileReadingFun.txt" For Input As 1
If Not EOF(1) Then
        Input #1, intId, strname, strAddress, strBirthDate, strPayrollType
        If intId = 2 Then   ' or whatever you test for
            Debug.Print CStr(intId); strname; strAddress; strBirthDate; strPayrollType
            '   or whatever you're going to do with your data
        End If
    Loop Until EOF(1)
End If
Close #1

Depending on how you open and read your file, you have various options as far as writing changes and such. You'll have to figure that part out yourself. I suggest you spend some time looking at the OPEN statement in the VB Help File.

That being said, I would question your choice of using a regular text file to do this. Have you considered using a DBMS such as MSAccess, mySQL or MSSQL? Or, if that's not an option, how about reading your entire text file into an ADO Recordset object in memory, doing your manipulations there, then write the whole dataset back to a text file when you're done? There are lots of options.

didn't you also ask this question in the VB.Net forum, which are you using.

it was my mistake, I'm using VB 6

open and read example two way to read input and line input method
input method read variable one by one line input read all variable at the same time

Open App.Path & "\example.txt" For Input As #3

While Not EOF(3)

On Error Resume Next

Input #3, a1, a2 (Line Input #3, a this is read i line )


Close #3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.