Results 1 to 14 of 14

Thread: [RESOLVED] Reading from text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Resolved [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..

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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".
    Last edited by cssriraman; Apr 22nd, 2006 at 02:51 AM.
    CS

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Reading from text file

    Here is the complete code to read last line of the text file:
    VB Code:
    1. Public Function fnReadLastLine(strFileName As String)
    2. 'You can call this function as:
    3. 'Call fnReadLastLine("C:\a.txt")
    4.  
    5.     Dim fPos As Long, LastLine As String, aByte As Byte
    6.     Dim testCRLF As String * 2
    7.     Const LF = 10
    8.  
    9.     On Error GoTo fnReadLastLine_Error
    10.  
    11.     Open strFileName For Binary As #1
    12.     fPos = LOF(1) + 1
    13.     Do
    14.         fPos = fPos - 2
    15.         Seek #1, fPos
    16.         Get #1, , testCRLF
    17.     Loop While testCRLF = vbCrLf
    18.     Do
    19.         fPos = fPos - 1
    20.         Seek #1, fPos
    21.         Get #1, , aByte
    22.     Loop Until aByte = LF
    23.     Line Input #1, LastLine
    24.     Close #1
    25.     MsgBox Trim$(LastLine)
    26.  
    27. ExitHere:
    28.     On Error GoTo 0
    29.     Exit Function
    30.  
    31. fnReadLastLine_Error:
    32.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1"
    33. End Function
    34.  
    35. Private Sub Form_Load()
    36. Dim strTemp() As String
    37. strTemp = Split(fnReadLastLine("C:\Temp\a.txt"), ",")
    38. Form1.Caption = Replace(strTemp(1), """", "")
    39. End Sub
    CS

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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?
    Attached Files Attached Files

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Reading from text file

    Here is the Updated code:
    VB Code:
    1. Public Function fnReadLastLine(strFileName As String)
    2. 'You can call this function as:
    3. 'Call fnReadLastLine("C:\a.txt")
    4.  
    5.     Dim fPos As Long, LastLine As String, aByte As Byte
    6.     Dim testCRLF As String * 2
    7.     Const LF = 10
    8.  
    9.     On Error GoTo fnReadLastLine_Error
    10.  
    11.     Open strFileName For Binary As #1
    12.     fPos = LOF(1) + 1
    13.     Do
    14.         fPos = fPos - 2
    15.         Seek #1, fPos
    16.         Get #1, , testCRLF
    17.     Loop While testCRLF = vbCrLf
    18.     Do
    19.         fPos = fPos - 1
    20.         Seek #1, fPos
    21.         Get #1, , aByte
    22.     Loop Until aByte = LF
    23.     Line Input #1, LastLine
    24.     Close #1
    25.     fnReadLastLine = Trim$(LastLine)
    26.  
    27. ExitHere:
    28.     On Error GoTo 0
    29.     Exit Function
    30.  
    31. fnReadLastLine_Error:
    32.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1"
    33. End Function
    34.  
    35. Private Sub Command1_Click()
    36.     Dim strTemp() As String
    37.     strTemp = Split(fnReadLastLine("c:\Kbengg\Estimate\DATABASE\test1.DAT"), ",")
    38.     Form1.Caption = Replace(strTemp(1), """", "")
    39. End Sub
    CS

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Reading from text file

    you may try this:
    VB Code:
    1. Private Function getDate(flname As String) As String
    2.  
    3. Dim tmpArr() As String
    4.     Open "c:\windows\desktop\test.txt" For Input As #1
    5.         tmpArr = Split(Input(LOF(1), 1), vbCrLf)
    6.         getDate = Split(tmpArr(UBound(tmpArr)), ",")(1)
    7.     Close #1
    8.    
    9. End Function
    10.  
    11. Private Sub Form_Load()
    12.     Dim flname As String
    13.     flname = "c:\windows\desktop\test.txt"
    14.     Me.Caption = getDate(flname)
    15. End Sub
    Harsh
    Last edited by Harsh Gupta; Apr 23rd, 2006 at 07:22 AM.
    Show Appreciation. Rate Posts.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Reading from text file

    Quote Originally Posted by Harsh Gupta
    you may try this:
    VB Code:
    1. Private Function getDate(flname As String) As String
    2.  
    3. Dim tmpArr() As String
    4.     Open "c:\windows\desktop\test.txt" For Input As #1
    5.         tmpArr = Split(Input(LOF(1), 1), vbCrLf)
    6.         [B]getDate = Split(tmpArr(UBound(tmpArr)), ",")(1)[/B]
    7.     Close #1
    8.    
    9. End Function
    10.  
    11. Private Sub Form_Load()
    12.     Dim flname As String
    13.     flname = "c:\windows\desktop\test.txt"
    14.     Me.Caption = getDate(flname)
    15. End Sub
    Harsh
    Shouldn't it be?...

    VB Code:
    1. getDate = Split(tmpArr(UBound(tmpArr)), ",", 1)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    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
    Attached Files Attached Files

  11. #11
    Hyperactive Member Bearnerd's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    290

    Re: Reading from text file

    Hi, try this code! Just a simple code but it works.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim getDate, tmpArr As String
    3.     Open App.Path & "\test1.txt" For Input As #1
    4.         While Not EOF(1)
    5.             Input #1, tmpArr, getDate
    6.             Me.Caption = getDate
    7.         Wend
    8.     Close #1
    9. End Sub

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading from text file

    lol.. funny how everyone has a different approach

    VB Code:
    1. Private Sub SetCaption(fName As String)
    2.     Dim tmp() As String
    3.     Dim sData() As String
    4.     Open fName For Input As #1
    5.         tmp = Split(Input(LOF(1), 1), vbCrLf)
    6.     Close #1
    7.     For x = UBound(tmp) To 0 Step -1
    8.         If tmp(x) <> "" Then
    9.             sData = Split(tmp(x), ",")
    10.             Me.Caption = Replace(sData(UBound(sData)), Chr(34), "")
    11.             Exit Sub
    12.         End If
    13.     Next
    14. End Sub
    15.  
    16.  
    17. Private Sub Form_Load()
    18.     SetCaption "c:\kbengg\estimate\database\data.dat"
    19. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reading from text file

    Many many thanks Harsha, Bearnerd & static.
    Thanks for the timely help.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width