Recordset2.Bookmark Property (DAO)

Sets or returns a bookmark that uniquely identifies the current record in a Recordset object.

Syntax

expression .Bookmark

expression A variable that represents a Recordset2 object.

Remarks

For a Recordset object based entirely on Microsoft Access database engine tables, the value of the Bookmarkable property is True, and you can use the Bookmark property with that recordset. Other database products may not support bookmarks, however. For example, you can't use bookmarks in any Recordset2 object based on a linked Paradox table that has no primary key.

When you create or open a Recordset object, each of its records already has a unique bookmark. You can save the bookmark for the current record by assigning the value of the Bookmark property to a variable. To quickly return to that record at any time after moving to a different record, set the Recordset object's Bookmark property to the value of that variable.

There is no limit to the number of bookmarks you can establish. To create a bookmark for a record other than the current record, move to the desired record and assign the value of the Bookmark property to a String variable that identifies the record.

To make sure the Recordset object supports bookmarks, check the value of its Bookmarkable property before you use the Bookmark property. If the Bookmarkable property is False, the Recordset object doesn't support bookmarks, and using the Bookmark property results in a trappable error.

If you use the Clone method to create a copy of a Recordset object, the Bookmark property settings for the original and the duplicate Recordset objects are identical and can be used interchangeably. However, you can't use bookmarks from different Recordset objects interchangeably, even if they were created by using the same object or the same SQL statement.

If you set the Bookmark property to a value that represents a deleted record, a trappable error occurs.

The value of the Bookmark property isn't the same as a record number.

Example

This example uses the Bookmark and Bookmarkable properties to let the user flag a record in a recordset and return to it later.

Sub BookmarkX() 
 
 Dim dbsNorthwind As Database 
 Dim rstCategories As Recordset2 
 Dim strMessage As String 
 Dim intCommand As Integer 
 Dim varBookmark As Variant 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 Set rstCategories = _ 
 dbsNorthwind.OpenRecordset("Categories", _ 
 dbOpenSnapshot) 
 
 With rstCategories 
 
 If .Bookmarkable = False Then 
 Debug.Print "Recordset is not Bookmarkable!" 
 Else 
 ' Populate Recordset. 
 .MoveLast 
 .MoveFirst 
 
 Do While True 
 ' Show information about current record and get 
 ' user input. 
 strMessage = "Category: " & !CategoryName & _ 
 " (record " & (.AbsolutePosition + 1) & _ 
 " of " & .RecordCount & ")" & vbCr & _ 
 "Enter command:" & vbCr & _ 
 "[1 - next / 2 - previous /" & vbCr & _ 
 "3 - set bookmark / 4 - go to bookmark]" 
 intCommand = Val(InputBox(strMessage)) 
 
 Select Case intCommand 
 ' Move forward or backward, trapping for BOF 
 ' or EOF. 
 Case 1 
 .MoveNext 
 If .EOF Then .MoveLast 
 Case 2 
 .MovePrevious 
 If .BOF Then .MoveFirst 
 
 ' Store the bookmark of the current record. 
 Case 3 
 varBookmark = .Bookmark 
 
 ' Go to the record indicated by the stored 
 ' bookmark. 
 Case 4 
 If IsEmpty(varBookmark) Then 
 MsgBox "No Bookmark set!" 
 Else 
 .Bookmark = varBookmark 
 End If 
 
 Case Else 
 Exit Do 
 End Select 
 
 Loop 
 
 End If 
 
 .Close 
 End With 
 
 dbsNorthwind.Close 
 
End Sub 

This example uses the LastModified property to move the current record pointer to both a record that has been modified and a newly created record.

Sub LastModifiedX() 
 
 Dim dbsNorthwind As Database 
 Dim rstEmployees As Recordset2 
 Dim strFirst As String 
 Dim strLast As String 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 Set rstEmployees = _ 
 dbsNorthwind.OpenRecordset("Employees", _ 
 dbOpenDynaset) 
 
 With rstEmployees 
 ' Store current data. 
 strFirst = !FirstName 
 strLast = !LastName 
 ' Change data in current record. 
 .Edit 
 !FirstName = "Julie" 
 !LastName = "Warren" 
 .Update 
 ' Move current record pointer to the most recently 
 ' changed or added record. 
 .Bookmark = .LastModified 
 Debug.Print _ 
 "Data in LastModified record after Edit: " & _ 
 !FirstName & " " & !LastName 
 
 ' Restore original data because this is a demonstration. 
 .Edit 
 !FirstName = strFirst 
 !LastName = strLast 
 .Update 
 
 ' Add new record. 
 .AddNew 
 !FirstName = "Roger" 
 !LastName = "Harui" 
 .Update 
 ' Move current record pointer to the most recently 
 ' changed or added record. 
 .Bookmark = .LastModified 
 Debug.Print _ 
 "Data in LastModified record after AddNew: " & _ 
 !FirstName & " " & !LastName 
 
 ' Delete new record because this is a demonstration. 
 .Delete 
 .Close 
 End With 
 
 dbsNorthwind.Close 
 
End Sub