Link to home
Start Free TrialLog in
Avatar of KingSencat
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"
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

' 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
   '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

' 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
   if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
   sText = sText & iif(len(stext)>0," & ","") & mid(sNextLine,5)
 
Loop
Text1.Text = sText
 
' Close the file
Close nFileNum

Open in new window

Avatar of KingSencat
KingSencat

ASKER

Are you sure this line is correct?

   if left(sNextLine,2) = "Day 1" or left(sNextLine,5) = "Day 1"
if left(sNextLine,5) = "Day 2" or left(sNextLine,5) = "Day 5"
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 !
' 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

Open in new window

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:
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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial