MoveFirst, MoveLast, MoveNext, MovePrevious Methods Example (MDB)

The following example uses the MoveLast method to populate the Recordset object so the number of records can be counted. The MoveFirst method then moves the current record pointer to the first record in the Recordset object. The procedure prompts the user to enter a number, then moves that number of records forward.

Sub MoveThroughRecords()
    Dim dbs As Database, rst As Recordset, intI As Integer
    Dim strNumber As String
    
    ' Return reference to current database.
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("Products")
    ' Populate Recordset object.
    rst.MoveLast
    ' Return to first record.
    rst.MoveFirst
    ' Get number smaller than number of records.
    strNumber = InputBox("Please enter a number less than " _
        & rst.RecordCount & ".")
    ' Move specified number of records.
    For intI = 1 To strNumber
        rst.MoveNext
    Next intI
    Debug.Print rst!ProductName
    rst.Close
    Set dbs = Nothing
End Sub