The Move Methods

When a recordset is first opened, the current record pointer is positioned on the first record in the recordset. The Move methods are used to navigate through the records in an open recordset. They do this by repositioning the Recordset object's current record pointer. The following Move methods are used in this chapter:

□ MoveFirst — Positions the current record pointer on the first record of the recordset.

□ MoveNext — Positions the current record pointer to the next record in the recordset.

The following code sample demonstrates common recordset navigation handling:

' Verify that the Recordset contains data. If Not rsData.EOF Then

' Loop until we reach the end of the Recordset. Do While Not rsData.EOF

' Perform some action on the current record's data. Debug.Print rsData.Fields(0).Value ' Move to the next record. rsData.MoveNext



MsgBox "Error, no records returned.", vbCritical End If

Pay particular attention to the use of the MoveNext method within the Do While loop. Omitting this is a very common error and will lead to an endless loop condition in your code. The very first line of code that you should place in the Do While loop is the call to MoveNext.

0 0

Post a comment