Introduction.
Bookmarks are stored on the record of a Form's Recordset when the Form is loaded into memory. When a Table or Query linked to a Form is open, a unique Id (of String Type) is generated and stored in the Bookmark Property of each record. When you close the Form, the Bookmark Property value is cleared. Bookmarks are two Byte data of String Type. They are not displayable or printable characters, when printed on the screen, it simply displays a question mark symbol.
Not all Recordsets are Bookmarked and this can be checked by reading its Bookmarkable Property Value. If the Bookmarkable Property value is False, then this Recordset doesn’t have bookmarks.
If you create a Recordsetclone in VBA from a Form’s (say Form-A) Recordset the Recordsetclone’s bookmark and Form’s Recordset bookmarks will be identical. You can use the StrComp() Function to compare Bookmarks. Use 0 (zero) as the third argument of the StrComp() Function.
But, if you load the same Table on a different Form (say Form-B) at the same time both form’s Recordset bookmarks will not be identical. When you close and open the Form with the same table a second time both session’s bookmarks of records will not be identical.
When a Linked Table having no Primary Key is open in a Form, then that Recordset will not have any bookmarks.
When Form doesn’t have a Record Source Value then addressing Form’s Bookmark Property will trigger an error. But, when a Table or Query is loaded into the Record Source property of the Form, the Form will have a Bookmark Property only for the Current Record. You can move the records on the Form and read their bookmarks and save them into different Variables to come back to those records later through VBA.
Sample Bookmark Based Trial Run.
Let us try a simple example to save the Bookmark of a record on the Form into a variable and use it later to come back to the bookmarked record.
Import the Employees Table from the Northwind sample database.
Create a Tabular Form for Employees Table.
On the Footer Section of the Form create two Command Buttons.
Select the first Command Button.
Display its Property Sheet (F4).
Change the Name Property value to cmdSave.
Change the Name Property value of the second Command Button to cmdRestore.
Display the VBA Module of the Employees Form.
Copy and Paste the following code into the VBA Module:
Dim bkMark As Variant Private Sub cmdRestore_Click() Me.Bookmark = bkMark MsgBox "Bookmark Restored" End Sub Private Sub cmdSave_Click() bkMark = Me.Bookmark MsgBox "Bookmark saved" End Sub
Save and Close the Form.
Open the Form in normal view showing employee records.
Use the record navigation control to move to the 5th record.
Click on the Save Command button to save the Bookmark of the current record in bkMark Variable.
Now, move a few records forward on the Form.
Click on the Restore Command Button to quickly make the 5th record current on the Form, by copying the Bookmark from bkMark Variable into the Form’s Bookmark Property. You can try this out with different records on the Form.
The following links will show you more tricks on this topic with interesting examples: