We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 17,360 times

Related Categories

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [1132] When to use MoveFirst

    Posted by Chazlenor on 08 Jun 2007

    I am using quite a large piece of code that includes both MoveFirst and MoveNext.  I am having problems with trying to get the code to return values from the second group that I've created. ...