• Why not seperate lines? (Access 97 VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Why not seperate lines? (Access 97 VBA)

    Author
    Topic
    #383632

    I am attempting to import this attached file using the following code:

    Sub ImportTest()
        'Open the file to import
        Dim intFileNum As Integer
        intFileNum = FreeFile
        
        Open "C:ebill_R_Audit_11_20021210.txt" For Input As #intFileNum
        
        Dim strFromFile As String
        
        While Not EOF(intFileNum)
            Line Input #intFileNum, strFromFile
            Debug.Print "zzz"
            Debug.Print strFromFile
            MsgBox strFromFile
        Wend
        Close #intFileNum
    End Sub

    Why do I get only 1 msgbox with the full contents of the file and not 3 with 1 line each?

    Viewing 1 reply thread
    Author
    Replies
    • #654875

      The problem is the text file.

      The lines are not separated by carrige returns and line feeds (chr(13)+chr(10)). They are only separated by line feeds.(chr(10))

      From the help on Line Input#:


      The Line Input # statement reads from a file one character at a time until it encounters a carriage return (Chr(13)) or carriage return

      • #654936

        I opened the data file in Word 2k and saved it back. Word replaced the linefeeds with cr/lf and the routine processed the file just fine.

        • #655201

          That’s a great idea. I routinely use WordPad to open .txt files that Notepad cannot parse due to nonstandard line terminators. Word is better for the poster because it can be automated.

          • #657399

            I ended up writing a work-around:

            Sub ImportTest()
                'Open the file to import
                Dim intFileNum As Integer
                intFileNum = FreeFile
                
                
                   Open "Myfile.txt" For Input As #intFileNum
                
                Dim strFromFile As String
                
                While Not EOF(intFileNum)
                    Line Input #intFileNum, strFromFile
                    Dim intLength
                    For intLength = 1 To Len(strFromFile)
                        Dim strIndividualRecord As String
                        If Asc(Mid(strFromFile, intLength, 1)) = 10 Or _
                           Asc(Mid(strFromFile, intLength, 1)) = 13 Then
                            'End of the record
                            If Len(strIndividualRecord) > 0 Then
                                Debug.Print "-----------"
                                Debug.Print strIndividualRecord
                            End If
                            
                            strIndividualRecord = ""
                        Else
                            strIndividualRecord = strIndividualRecord + Mid(strFromFile, intLength, 1)
                        End If
                    Next intLength
                Wend
                Close #intFileNum
                MsgBox "Done!"
            End Sub

            Thanks for all the suggestions!

    • #654922

      See my Post #138639 for some suggestions on using the FileSystemObject methods to parse text files. Of course, there is much more here in the Lounge on that…

      Afterthought: Because you are using Access 97, which is VBA 5.x, you do not natively have the Split() function, which is the perfect tool for turning a blob of delimited text into a well-ordered array. But I wonder if it’s possible to tap the Split() function through the Scripting runtime? That would be sweet. smile

    Viewing 1 reply thread
    Reply To: Why not seperate lines? (Access 97 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: