Solved

Working with a text file

Posted on 2004-08-16
5
149 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 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

16 Experts available now in Live!

Get 1:1 Help Now