KingSencat
asked on
Input Data From .TXT File ( VB 6.0 and 500 Points )
Hi experts, i am trying to get some data from a .txt file back to my vb 6.0 project. My .txt file contains apprx the following:
Day 1: At School
Day 2: At work
Day 3: At Home
Day 4: With Friends
Day 5: In the club
Day 6: ............
Day 7: .............
How do i get the results of Day 2 and Day 5 back to my vb 6.0 project?
Example:
Msgbox "At work" & "In the club"
Day 1: At School
Day 2: At work
Day 3: At Home
Day 4: With Friends
Day 5: In the club
Day 6: ............
Day 7: .............
How do i get the results of Day 2 and Day 5 back to my vb 6.0 project?
Example:
Msgbox "At work" & "In the club"
ASKER
Are you sure this line is correct?
if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
if left(sNextLine,5) = "Day 2" or left(sNextLine,5) = "Day 5"
ASKER
i have tried your first code but what I need is to get only the results of the days. and not all the .txt file
I thought you only needed those 2.
Code below will read line by line and give a messagebox for each line.
You can do with it everything you want !
Code below will read line by line and give a messagebox for each line.
You can do with it everything you want !
' Get a free file number
nFileNum = FreeFile
' Open a text file for input. inputbox returns the path to read the file
Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
Line Input #nFileNum, sNextLine
msgbox sNextLine
Loop
' Close the file
Close nFileNum
The following reads the whole file and uses Split() to place each line into a separate entry in an Array.
Then you can easily access whatever line you want:
Then you can easily access whatever line you want:
Dim ff As Integer
Dim fileName As String
Dim entireFile As String
Dim lines() As String
ff = FreeFile
fileName = "c:\someFile.txt"
Open fileName For Binary Access Read As #ff
entireFile = Input(LOF(ff), ff)
Close #ff
lines = Split(entireFile, vbCrLf)
' Example: To get the second line:
Dim line2 As String
line2 = Trim(Split(lines(1), ":")) ' <-- Zero based Array
MsgBox "Line 2 Value is: " & line2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
nFileNum = FreeFile
' Open a text file for input. inputbox returns the path to read the file
Open "C:\Documents and Settings\Sudeep\My Documents\3.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
Line Input #nFileNum, sNextLine
'do something with it
'add line numbers to it, in this case!
sNextLine = sNextLine & vbCrLf
sText = sText & sNextLine
Loop
Text1.Text = sText
' Close the file
Close nFileNum
Open in new window