When to use MoveFirst

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.

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay