Marking Record Positions with Bookmarks

A bookmark is a system-generated Byte array that uniquely identifies each record. The Bookmark property of a Recordset object changes each time you move to a new record. To identify a record, move to that record and then assign the value of the Bookmark property to a variable of type Variant. To return to the record, set the Bookmark property to the value of the variable.

The following example illustrates how you can use a bookmark to save the current record position and then quickly return to that record position if a Find or Seek method fails. Using a bookmark in this way is useful, because if the method fails, the current record position is undefined. In this example, strDbPath is the path to the NorthwindTables database:

Dim dbs As Database, rst As Recordset
Dim varOrigin As Variant

Set dbs = OpenDatabase(strDbPath)
' Open table-type recordset.
Set rst = dbs.OpenRecordset("Customers", dbOpenTable)
With rst
	' Set recordset index.
	.Index = "CompanyName"
	MsgBox "Current Record: CompanyName = " & !CompanyName
	' Return bookmark.
	varOrigin = .Bookmark
	' Perform seek.
	.Seek ">=", "Z"
	' Check for match.
	If .NoMatch Then
		MsgBox "Can't find a company name starting with 'Z'. ", _
			vbOKOnly, "There is no current record!"
		' Set bookmark.
		.Bookmark = varOrigin
	End If
	MsgBox "Current Record: CompanyName = " & !CompanyName
	.Close
End With

In this example, the Bookmark property for the current record is saved, and if the Seek method fails, the Bookmark property is reset to its previous value.

The LastModified property of the Recordset object provides a good illustration of how to use a bookmark. The LastModified property is the bookmark of the last record in the Recordset object to be added or modified. To use it, set the Recordset object’s Bookmark property equal to the LastModified property as follows:

rst.Bookmark = rst.LastModified

This moves the current record position to the last record that was added or modified. This is particularly useful when you are adding new records, because by default, the current record position after you add a new record is the record you were on before using the AddNew method. With the LastModified property, you can easily make the newly added record the current record if that’s what your application expects.