In early versions of DAO, after opening a recordset it was necessary
to use the MoveFirst method to move the cursor to the first record
before looping through the recordset. ADO and DAO have removed the
need to issue MoveFirst before looping through a recordset, however.
That's because after opening a recordset that contains records, both
these engines automatically move the cursor to the first record. If
the recordset is empty, then they set the BOF and EOF properties to
True. Even so, we often see code examples on the message boards still
issuing MoveFirst before looping through a recordset.
Instead, you can reliably use the EOF property to determine if the
recordset contains records, manipulate the record as you please, then
issue the MoveNext command before looping through the structure. Using
the latest DAO engine in Access, the simplified code might be:
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblCategories")
With rst
Do Until .EOF
Debug.Print .Fields(1)
.MoveNext
Loop
.Close
End With
Set rst = Nothing
For ADO, you'd create and open the recordset differently, of course,
but the loop structure would remain the same.
Comments