The Recordset's five most common methods that enable you to programmatically position
the record pointer are as follows:
-
Move - This method takes a positive or negative Long value
as a required parameter. The parameter specifies the number of records to move
away from the current record pointer position. Positive values indicate forward
movement, while negative values indicate backward movement. An optional second
parameter enables you to specify the Bookmark of a different record. Specifying
this second parameter causes the movement to happen relative to the record of
the Bookmark.
-
MoveFirst - Moves the record pointer to the first row of the
Recordset's data.
-
MoveLast - Moves the record pointer to the last row of the
Recordset's data.
-
MoveNext - Moves the record pointer one row beyond its current
position in the Recordset.
-
MovePrevious - Moves the record pointer one row before its
current position in the Recordset.
You might call these methods to programmatically process records, or you might
call them in response to some user action, such as clicking buttons labeled Next,
Previous, First, or Last.
It is possible to move the record pointer too far (that is, past the beginning
or end of the Recordset) with the Move, MoveNext, and MovePrevious methods. To
help you avoid this problem, each Recordset has "buffer records" just before its
first row and just after its last row. When you move the record pointer onto one
of the beginning or ending buffer records, no error happens, but the Recordset's
Boolean property BOF (Beginning-of-file) or EOF (Endof- file) becomes True.
You should always test the BOF property immediately after calling the MovePrevious
method and the EOF property after every call to MoveNext, and you should test
one or both of the properties after calling the Move method. The examples in Listing
8.10 present code that you might put in the Click event procedures for Next and
Previous CommandButtons. (Notice the call to ReadFromData, a procedure the programmer
has written to populate controls with field data from the Recordset's copy buffer.)
LISTING 8.10
USING THE EOF AND BOF PROPERTIES WITH MOVENEXT AND MOVEPREVIOUS
Private Sub cmdNext_Click()
rsEmployees.MoveNext
If rsEmployees.EOF Then
rsEmployees.MoveLast
EndIf
ReadFromData
End Sub
Private Sub cmdPrevious_Click()
rsEmployees.MovePrevious
If rsEmployees.BOF Then
rsEmployees.MoveFirst
EndIf
ReadFromData
End Sub
If you programmatically loop through a Recordset, you must also check for the
EOF property. You can perform this type of navigation by writing a loop that keeps
advancing the record pointer with MoveNext until EOF is True. An example of a
record-processing loop might look like the code in Listing 8.11.
LISTING 8.11
A RECORD-PROCESSING LOOP
rsEmployees.MoveFirst
Do Until rsEmployees.EOF
'...some code to process a record
rsEmployees.MoveNext
Loop
In this example, you always start at the first record in the rsEmployees Recordset
and go through the entire Recordset with MoveNext.