1,105,633 Community Members

Searching and reading a line from a text file

Member Avatar
BeeKeeper18
Newbie Poster
9 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
BitBlt
Practically a Posting Shark
895 posts since Feb 2011
Reputation Points: 442 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

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.

Member Avatar
ChrisPadgham
Posting Pro in Training
450 posts since Sep 2009
Reputation Points: 113 [?]
Q&As Helped to Solve: 90 [?]
Skill Endorsements: 8 [?]
 
0
 

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

Member Avatar
BeeKeeper18
Newbie Poster
9 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

it was my mistake, I'm using VB 6

Member Avatar
kaozci
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: