| << 13.3.2- Moving Through Records | Chapter13 | 13.3.4- Finding Records >> |
Bookmarks
A bookmark is really quite intuitive – it's a way of marking a particular record in the recordset. In fact it uniquely identifies a record within the recordset so that you can make your cursor jump straight back to it, from any other place in the recordset.
A bookmark is stored as a variant value (although you'll probably never want to view the bookmark itself, as its value is really only meaningful to ADO). There are several important facts to note about bookmarks:
- It's possible to have two bookmarks that point at the same record but have different values. This means that you can't compare bookmarks directly.
- You can create two similar or identical recordsets from the same source, and set a bookmark on the same record of each recordset. But the bookmarks are not the same
- Some types of recordset don't support bookmarks. You can find out whether your recordset supports bookmarks by using the Supports method (the expression objRS.Supports(adBookmark) returns a Boolean value)
Using Bookmarks
The following section of code demonstrates how we might use bookmarks:
Dim varMyBookmark ' create a variant to hold the bookmark
... ' code to create the Recordset object
varMyBookmark = objRS.Bookmark ' later, set a bookmark at the current record,
' and save it to varMyBookmark
... ' some processing of records
objRS.Bookmark = varMyBookmark ' later still, move the cursor to the record
' specified in the varMyBookmark bookmark
First, this creates a variant called varMyBookmark. Later, we use the Bookmark property to set a bookmark at the current cursor position – the bookmark is stored in the varMyBookmark variant. Later still, we want to return the cursor to the bookmarked record – so we set the recordset's Bookmark property back to the value contained in varMyBookmark.
Bookmarks and the Move Method
We've already seen how we can use the Move method to move the cursor a specified number of places from its current position. But as we mentioned there, we can use the Move method along with a bookmark too. This allows us to move the cursor to any bookmarked record, and then move it from there by a specified number of records. For example, consider the following:
objRS.Move 3, varMyBookmark
This moves the cursor to the third record after the record bookmarked by varMyBookmark. Here, we're using the Move method's second parameter to specify the starting point for the move, and the first parameter to specify the offset. So, effectively this says " move the cursor forwards three places, using the record bookmarked by varMyBookmark as a starting position".
As we saw earlier, we can use the Move method without specifying the second parameter – in that case, the Move method adopts its default behavior of moving the cursor from its current position. There are some other special predefined bookmarks that you can use in the second parameter:
|
Value |
Meaning |
|
adBookmarkCurrent |
Use the current record as the starting position |
|
adBookmarkFirst |
Use the first record as the starting position |
|
adBookmarkLast |
Use the last record as the starting position |
Note that we don't need to create these bookmarks. These values are ADO constants, and are predefined in the ADO constant library msado15.dll.
For example, to move three records further on from the current position you would use this:
objRS.Move 3, adBookmarkCurrent
or this:
objRS.Move 3
So, to move to the fourth record in the recordset we'd use the following:
objRS.Move 3, adBookmarkFirst
To move to the penultimate record in the recordset we'd use this:
objRS.Move -1, adBookmarkLast
If you attempt to move beyond the beginning or end of a recordset then BOF or EOF is set accordingly.
| << 13.3.2- Moving Through Records | Chapter13 | 13.3.4- Finding Records >> |

RSS

