[RESOLVED] Reading from text file
Hi,
I have a text file (Data.dat) containing the following.
MasterFile, Date
c:\kbengg\estimate\database\01122000.dat,"On 01-Dec-2000"
c:\kbengg\estimate\database\01012002.dat,"On 01-Jan-2002"
c:\kbengg\estimate\database\01012006.dat,"On 01-Jan-2006"
In VB 6.0, I have a form; Is it possible to set form.caption to the value read from the text file.
The program should go to the last line of the text file "Data.dat" and read the value in the quotes.
For example form.caption ="On 01-Jan-2006"
The data.dat file is in c:\kbengg\estimate\database\
I will thankful for any help..
Re: Reading from text file
First you have to read the last line of the text file. then use the functions like replace, split to get "On 01-Jan-2006".
Re: Reading from text file
Here is the complete code to read last line of the text file:
VB Code:
Public Function fnReadLastLine(strFileName As String)
'You can call this function as:
'Call fnReadLastLine("C:\a.txt")
Dim fPos As Long, LastLine As String, aByte As Byte
Dim testCRLF As String * 2
Const LF = 10
On Error GoTo fnReadLastLine_Error
Open strFileName For Binary As #1
fPos = LOF(1) + 1
Do
fPos = fPos - 2
Seek #1, fPos
Get #1, , testCRLF
Loop While testCRLF = vbCrLf
Do
fPos = fPos - 1
Seek #1, fPos
Get #1, , aByte
Loop Until aByte = LF
Line Input #1, LastLine
Close #1
MsgBox Trim$(LastLine)
ExitHere:
On Error GoTo 0
Exit Function
fnReadLastLine_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1"
End Function
Private Sub Form_Load()
Dim strTemp() As String
strTemp = Split(fnReadLastLine("C:\Temp\a.txt"), ",")
Form1.Caption = Replace(strTemp(1), """", "")
End Sub
1 Attachment(s)
Re: Reading from text file
Hi CS,
thank you for your response.
I am getting Runtime error 9 "Subscript out of range" error on the following line
Form1.Caption = Replace(strTemp(1), """", "")
I am attaching the project files for your ready reference...
Could you please check?
Re: Reading from text file
Here is the Updated code:
VB Code:
Public Function fnReadLastLine(strFileName As String)
'You can call this function as:
'Call fnReadLastLine("C:\a.txt")
Dim fPos As Long, LastLine As String, aByte As Byte
Dim testCRLF As String * 2
Const LF = 10
On Error GoTo fnReadLastLine_Error
Open strFileName For Binary As #1
fPos = LOF(1) + 1
Do
fPos = fPos - 2
Seek #1, fPos
Get #1, , testCRLF
Loop While testCRLF = vbCrLf
Do
fPos = fPos - 1
Seek #1, fPos
Get #1, , aByte
Loop Until aByte = LF
Line Input #1, LastLine
Close #1
fnReadLastLine = Trim$(LastLine)
ExitHere:
On Error GoTo 0
Exit Function
fnReadLastLine_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1"
End Function
Private Sub Command1_Click()
Dim strTemp() As String
strTemp = Split(fnReadLastLine("c:\Kbengg\Estimate\DATABASE\test1.DAT"), ",")
Form1.Caption = Replace(strTemp(1), """", "")
End Sub
Re: Reading from text file
you may try this:
VB Code:
Private Function getDate(flname As String) As String
Dim tmpArr() As String
Open "c:\windows\desktop\test.txt" For Input As #1
tmpArr = Split(Input(LOF(1), 1), vbCrLf)
getDate = Split(tmpArr(UBound(tmpArr)), ",")(1)
Close #1
End Function
Private Sub Form_Load()
Dim flname As String
flname = "c:\windows\desktop\test.txt"
Me.Caption = getDate(flname)
End Sub
Harsh
Re: Reading from text file
Hi Harsh,
thank you for your feedback.
I am getting Run-time error '9': Subscript out of range error on below line of code
getDate = Split(tmpArr(UBound(tmpArr)), ",")(1)
Can you please check?
Thanks
Re: Reading from text file
are you sure?? because it is working fine on my system!!
please post the exact code you used and also show the exact, whole contents of the file you are using.
Re: Reading from text file
Quote:
Originally Posted by Harsh Gupta
you may try this:
VB Code:
Private Function getDate(flname As String) As String
Dim tmpArr() As String
Open "c:\windows\desktop\test.txt" For Input As #1
tmpArr = Split(Input(LOF(1), 1), vbCrLf)
[B]getDate = Split(tmpArr(UBound(tmpArr)), ",")(1)[/B]
Close #1
End Function
Private Sub Form_Load()
Dim flname As String
flname = "c:\windows\desktop\test.txt"
Me.Caption = getDate(flname)
End Sub
Harsh
Shouldn't it be?...
VB Code:
getDate = Split(tmpArr(UBound(tmpArr)), ",", 1)
1 Attachment(s)
Re: Reading from text file
thank you Harsha and Dee-u,
I am attaching the project files along with the text file for your ready reference.
thanks
Re: Reading from text file
Hi, try this code! Just a simple code but it works. :wave:
VB Code:
Private Sub Form_Load()
Dim getDate, tmpArr As String
Open App.Path & "\test1.txt" For Input As #1
While Not EOF(1)
Input #1, tmpArr, getDate
Me.Caption = getDate
Wend
Close #1
End Sub
Re: Reading from text file
lol.. funny how everyone has a different approach
VB Code:
Private Sub SetCaption(fName As String)
Dim tmp() As String
Dim sData() As String
Open fName For Input As #1
tmp = Split(Input(LOF(1), 1), vbCrLf)
Close #1
For x = UBound(tmp) To 0 Step -1
If tmp(x) <> "" Then
sData = Split(tmp(x), ",")
Me.Caption = Replace(sData(UBound(sData)), Chr(34), "")
Exit Sub
End If
Next
End Sub
Private Sub Form_Load()
SetCaption "c:\kbengg\estimate\database\data.dat"
End Sub
Re: Reading from text file
surya,
you were getting the error because there was a line present (cursor was below the last line) after the actual last line. you may use Static's method. our ways are similar except that he took care of the problem.
Re: Reading from text file
Many many thanks Harsha, Bearnerd & static.
Thanks for the timely help.....