Solved

Working with a text file

Posted on 2004-08-16
5
151 Views
Last Modified: 2010-05-02
How would I read the 3rd line of a text file and put it into a variable?
0
Comment
Question by:pcarlow
  • 4
5 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11815561
Include a reference to the "Microsft Scripting Runtime" by going into the Project/References menu option.

Private Sub Command1_Click()

    Dim fso As Scripting.FileSystemObject
    Dim MyTextStream As Scripting.TextStream
    Dim sLineText As String
    Dim i As Long

    Set fso = New Scripting.FileSystemObject
    Set MyTextStream = fso.OpenTextFile("c:\a.txt")
    For i = 1 To 3
        sLineText = MyTextStream.ReadLine
    Next i
    MsgBox sLineText
   
End Sub
0
 
LVL 7

Accepted Solution

by:
Burbble earned 50 total points
ID: 11815623
Here you go:

-----
Private Sub Form_Load()
    Dim FileNo As Integer
    Dim strLine() As String
    Dim strTemp As String
    Dim intCounter As Integer

    intCounter = 0
    FileNo = FreeFile
    Open "C:\filename.txt" For Input As #FileNo
        Do While Not EOF(FileNo)
            Line Input #FileNo, strTemp
            ReDim Preserve strLine(intCounter)
            strLine(intCounter) = strTemp
            intCounter = intCounter + 1
        Loop
    Close #FileNo
End Sub
-----

-Burbble
0
 
LVL 7

Expert Comment

by:Burbble
ID: 11815624
Ah, two methods there ;-)

-Burbble
0
 
LVL 7

Expert Comment

by:Burbble
ID: 11817397
Oh, I should have mentioned that the third line would be:

strLine(2)

Since the array starts at 0.

-Burbble
0
 
LVL 7

Expert Comment

by:Burbble
ID: 11826924
Glad I could help, thanks for the points ;-)

-Burbble
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now