Search code examples
ms-accessvbams-access-2013recordset

VBA To Iterate RecordSet


I am using VBA to iterate a recordset, and when I use the RecordCount feature it returns 8 (which is accurate) - but when I use Debug.Print to print out the variable on each pass, only the first 3 variables are processed.

Why does this syntax stop short?

Set rs = Db.OpenRecordset("Select statement here)", dbOpenDynaset)
Debug.Print rs.RecordCount
'This prints 8
 With rs
  If Not .EOF And Not .BOF Then
     .MoveLast
     .MoveFirst
     Do While Not .EOF
        fakca = .Fields(0).Value
            Debug.Print fakca
            'only prints the first 3 in the table?
        .MoveNext
     Loop
  End If
End With

The .RecordCount will print 1, 2, 3101, 4, 5, 6, 7, 9 - but the Debug.Print fakca will only print 1, 2, 3101 and it stops


Solution

  • Try like this:

    With rs
        If Not .EOF Then
            .MoveFirst
            Do While Not .EOF
                fakca = .Fields(0).Value
                Debug.Print fakca
                .MoveNext
            Loop
        End If
    End With
    

    I think that the missundernstanding comes from the usage of this:

    If Not .EOF And Not .BOF Then
         .MoveLast
         .MoveFirst
    

    Thus I have avoided it. What is the idea of using .BOF?