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

Dani AI

Generated

A few practical notes to complement and : the basic idea is correct — read each line and test the ID — but for clarity and robustness in VB6 you’ll often get better results using the Scripting FileSystemObject (string-level control) instead of low-level Input # parsing. The example below shows a line-by-line reader that trims quotes/spaces and compares the first field (ID). It also makes it easy to display fields or plug them into controls.

' Requires a reference to "Microsoft Scripting Runtime" (Scrrun.dll)
Dim fso As New Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim line As String
Dim parts() As String
Dim targetId As String
targetId = "2"  ' change to the ID you want

Set ts = fso.OpenTextFile(App.Path & "\workers.txt", 1) ' 1 = ForReading
Do While Not ts.AtEndOfStream
    line = ts.ReadLine
    If Len(Trim(line)) > 0 Then
        parts = Split(line, ",")
        If UCase(Replace(Replace(Trim(parts(0)), """", ""), vbCr, "")) = UCase(targetId) Then
            ' found — e.g. parts(1)=Name, parts(2)=Address, parts(3)=Birthday, parts(4)=PayrollType
            Debug.Print "Found: ID=" & Trim(parts(0)) & " Name=" & Trim(parts(1))
            Exit Do
        End If
    End If
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing

To update or remove a record, write every non-matching (or modified) line to a temporary file and then replace the original file — this avoids fragile in-place edits. Example pattern: open source for reading, CreateTextFile for the temp, loop copying or changing lines, close both, delete original, move temp into place.

Troubleshooting tips: if fields can contain commas or quoted text, use a CSV parser (or ADO Jet/ACE CSV driver) instead of naive Split. Always Trim and strip quotes before comparisons, validate numeric conversions with error handling, and for anything beyond a few thousand rows consider a small database (Access/SQLite) for safer querying and updates.

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
    Do
        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 )

Wend

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.