AbsolutePosition Property

Applies To

Dynamic-Type Recordset object, Dynaset-Type Recordset object, Recordset object, Snapshot-Type Recordset object.

Description

Sets or returns the relative record number of a Recordset object's current record.

Settings and Return Values

The setting or return value is a Long integer from 0 to one less than the number of records in the Recordset object. It corresponds to the ordinal position of the current record in the Recordset object specified by the object.

Remarks

You can use the AbsolutePosition property to position the current record pointer to a specific record based on its ordinal position in a dynaset- or snapshot-type Recordset object. You can also determine the current record number by checking the AbsolutePosition property setting.

Because the AbsolutePosition property value is zero-based (that is, a setting of 0 refers to the first record in the Recordset object), you cannot set it to a value greater than or equal to the number of populated records; doing so causes a trappable error. You can determine the number of populated records in the Recordset object by checking the RecordCount property setting. The maximum allowable setting for the AbsolutePosition property is the value of the RecordCount property minus 1.

If there is no current record, as when there are no records in the Recordset object, AbsolutePosition returns –1. If the current record is deleted, the AbsolutePosition property value isn't defined, and a trappable error occurs if it's referenced. New records are added to the end of the sequence.

You shouldn't use this property as a surrogate record number. Bookmarks are still the recommended way of retaining and returning to a given position and are the only way to position the current record across all types of Recordset objects. In particular, the position of a record changes when one or more records preceding it are deleted. There is also no assurance that a record will have the same absolute position if the Recordset object is re-created again because the order of individual records within a Recordset object isn't guaranteed unless it's created with an SQL statement by using an ORDER BY clause.

Notes

  • Setting the AbsolutePosition property to a value greater than zero on a newly opened but unpopulated Recordset object causes a trappable error. Populate the Recordset object first with the MoveLast method.
  • The AbsolutePosition property isn't available on forward-only–type Recordset objects, or on Recordset objects opened from pass-through queries against Microsoft Jet-connected ODBC databases.
See Also

PercentPosition property, RecordCount property.

Example

This example uses the AbsolutePosition property to track the progress of a loop that enumerates all the records of a Recordset.

Sub AbsolutePositionX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim strMessage As String

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    ' AbsolutePosition only works with dynasets or snapshots.
    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", _
            dbOpenSnapshot)

    With rstEmployees
        ' Populate Recordset.
        .MoveLast
        .MoveFirst

        ' Enumerate Recordset.
        Do While Not .EOF
            ' Display current record information. Add 1 to
            ' AbsolutePosition value because it is zero-based.
            strMessage = "Employee: " & !LastName & vbCr & _
                "(record " & (.AbsolutePosition + 1) & _
                " of " & .RecordCount & ")"
            If MsgBox(strMessage, vbOKCancel) = vbCancel _
                Then Exit Do
            .MoveNext
        Loop

        .Close
    End With

    dbsNorthwind.Close

End Sub
Example (Microsoft Access)

The following example finds a particular record in a dynaset-type Recordset object and determines the ordinal position of that record:

Sub RecordOrdinalPosition()
    Dim dbs As Database, rst As Recordset

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to dynaset-type Recordset object object.
    Set rst = dbs.OpenRecordset("SELECT * FROM Orders WHERE " _
        & "ShippedDate >= #1-1-96#;")
    ' Find first order shipped to London.
    rst.FindFirst "ShipCity = 'London'"
    ' Return ordinal position of that record.
    Debug.Print rst.AbsolutePosition
    rst.Close
    Set dbs = Nothing
End Sub
Example (Microsoft Excel)

This example prompts the user for a record number. The example uses this number to move to a record in the Customer recordset in the Nwindex.mdb database, and then it copies the values for three specified fields to Sheet1.

To create the Nwindex.mdb database, run the Microsoft Excel example for the CreateDatabase method.

Dim db As Database, rs As Recordset
Sheets("Sheet1").Activate
recordNumber = Application.InputBox(Prompt:="Record number to copy " _
    & "to Sheet1", Title:="Record to copy", Type:=1)
If recordNumber = False Then    ' user cancelled InputBox
    Exit Sub
End If
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
Set rs = db.OpenRecordset("Customer", dbOpenSnapshot)
rs.MoveLast
If rs.RecordCount > recordNumber Then
    rs.AbsolutePosition = recordNumber
    ActiveCell.Value = rs.Fields("CONTACT").Value
    ActiveCell.Offset(, 1).Value = rs.Fields("ADDRESS").Value
    ActiveCell.Offset(, 2).Value = rs.Fields("CITY").Value
Else
    MsgBox "The record #" & recordNumber & " doesn't exist."
End If
rs.Close
db.Close