ADO - MoveNext &MovePrevious
1. MoveNext
-
Purpose: Moves the record pointer forward to the next record in the Recordset.
-
Syntax:
rs.MoveNext -
Usage Notes:
-
Used when looping through all records, typically with a
Do Until rs.EOFloop. -
If the current record is the last record, calling
MoveNextmoves the pointer past the last record — settingEOF = True. -
Always check for
EOFafter moving.
-
Example:
Set rs = conn.Execute("SELECT * FROM Employees")
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Do Until rs.EOF
MsgBox rs("EmployeeName")
rs.MoveNext
Loop
End If
2. MovePrevious
-
Purpose: Moves the record pointer backward to the previous record in the Recordset.
-
Syntax:
rs.MovePrevious -
Usage Notes:
-
Used when you want to move backward through the records.
-
If the current record is the first record, calling
MovePreviousmoves the pointer before the first record — settingBOF = True. -
Can only be used if the Recordset supports backward movement (not possible with
adOpenForwardOnly).
-
Example:
rs.Open "SELECT * FROM Employees", conn, adOpenStatic, adLockReadOnly
rs.MoveLast
Do Until rs.BOF
MsgBox rs("EmployeeName")
rs.MovePrevious
Loop
3. Cursor Type Requirement
-
Both
MoveNextandMovePreviousdepend on the cursor type:-
MoveNext→ works with all cursor types. -
MovePrevious→ requires a scrollable cursor, such as:-
adOpenStatic -
adOpenKeyset -
adOpenDynamic
-
-
Example:
rs.Open "SELECT * FROM Employees", conn, adOpenStatic
4. Summary Table
| Method | Moves Pointer | Works with All Cursors | Sets Property When Out of Range |
|---|---|---|---|
MoveNext |
To next record | Yes | EOF = True when past last record |
MovePrevious |
To previous record | No (needs scrollable cursor) | BOF = True when before first record |