Solved

Working with a text file

Posted on 2004-08-16
5
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

626 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