Results 1 to 12 of 12
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    The fundamentals of loops

    I often times loop through DOA Recordsets with code structured like this:
    Code:
    Option Compare Database
    Option Explicit
    
    
    'blah blah blah
    
    
    With rs
    If .RecordCount > 0 Then
        .MoveFirst
        While Not .EOF
        'blah blah blah !fields
        .MoveNext
        Wend
    End If
    
    
    End With
    
    
    'blah blah blah
    Such loops never fail to process the last record before exiting the loop. That is, the loop does not terminate until the .MoveNext exhausts the recordset.

    I don't often find occasions to read txt files, but recently such an occasion arose and so my natural tendency was to code a similarly structured loop to process a file. Upon discovering within the app that I was missing the last record from the file, I wrote a little test sub to investigate:

    Click image for larger version. 

Name:	000.jpg 
Views:	17 
Size:	92.2 KB 
ID:	49487
    The text file has five (5) records within, 3 text lines with intervening blank lines. Unlike the code shown above, typically used to loop through DAO Recordsets, the loop reading the text file terminates the loop after reading in the last record rather than when an attempt is made to read another record. So, as one can see, the Debug.Print statement for the last record never fired.

    So, puzzled with it all, I changed the sequence of statement events and below is the result. Is there some explanation for this behavior where loops behave differently when reading files versus recordsets?

    Click image for larger version. 

Name:	001.jpg 
Views:	17 
Size:	89.8 KB 
ID:	49488

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    So you are expecting one record to a line?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Yes, I'm expecting 5 distinct records from the txt file.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    So post the file from notepad+ (with hidden characters), and see what is produced?
    Sounds like you are not delimiting any records correctly?

    https://www.google.com/search?q=what...obile&ie=UTF-8
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Dupe post in error.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Recordset EOF and BOF is true when you move beyond the last or first record. EOF with file is when you get to the end of the file, so not sure what you were expecting, especially since it looks like you have 2 blank lines in the output.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    EOF with file is when you get to the end of the file
    I wasn't expecting the EOF to fire until I attempted to read another record AFTER having processed the last record. With your explanation, it's clear my expectation was faulty given the sequence of the statements within the loop.

    Thanks, lesson learned.

    I think this is what Welshgasman was asking for: Click image for larger version. 

Name:	002.jpg 
Views:	17 
Size:	13.3 KB 
ID:	49490

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Ok,I was expecting crlf at the end of every line.
    So apprently for text files, no need for last line, eof takes priority?

    Easy enough to examine regardless?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I tested your code with your sample file and all lines are output.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    There was a sample file?, I missed that on my phone?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Not attached file but sample data in image.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I don't often find occasions to read txt files, but recently such an occasion arose
    Not sure what your recent occasion entailed but just wanted to point out that FileSystemObject has a text stream object which is pretty handy.

    Code:
    Sub OpenTextFileRead(sTextFilePath As String)
        ' Must add reference to Tools > References > Microsoft Scripting Runtime
        ' (1) Open an instance of the FileSystemObject.
        ' (2) Open an instance of the FileSystemObject TextStream.
        ' (3) Read each line of text in the TextStream object.
        ' (4) Close the TextStream object.
    
    
        Dim fso As Scripting.FileSystemObject
        Dim tsTxtFile As Scripting.TextStream
        
        ' (1) Open an instance of the FileSystemObject.
        Set fso = New Scripting.FileSystemObject
        
        ' (2) Open an instance of the FileSystemObject TextStream.
        Set tsTxtFile = fso.OpenTextFile(sTextFilePath, ForReading, False, TristateMixed)
        
        With tsTxtFile
        
            ' (3) Read each line of text in the TextStream object.
            Do Until .AtEndOfStream
                ' ************************************
                ' insert your code here
                ' ************************************
                Debug.Print .Line & ": " & .ReadLine ' remove in production
            Loop
    
    
            ' (4) Close the TextStream object.
            .Close
            
        End With
        
        Set fso = Nothing
    
    
    End Sub
    5 test lines with a CrLf after each entry.
    Code:
    1: test line 1
    2: 
    3: test line 2
    4: 
    5: test line 3
    6: 
    7: test line 4
    8: 
    9: test line 5
    10:
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. nest loops
    By slimjen in forum Programming
    Replies: 20
    Last Post: 04-06-2015, 06:38 AM
  2. Loops
    By Monica2 in forum Programming
    Replies: 8
    Last Post: 09-14-2011, 09:16 AM
  3. Loops
    By tbassngal in forum Forms
    Replies: 2
    Last Post: 09-01-2011, 11:06 AM
  4. Loops
    By cksm4 in forum Programming
    Replies: 14
    Last Post: 10-10-2010, 02:10 PM
  5. Help with Loops
    By rotorque in forum Access
    Replies: 0
    Last Post: 01-07-2009, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums