Solved

Working with a text file

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

752 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