編輯

共用方式為


Using bookmarks

Applies to: Access 2013, Office 2013

It is often useful to return directly to a specific record after having moved around in the Recordset without having to scroll through every record and compare values. For example, if you attempt to search for a record using the Find method but the search returns no records, you are automatically placed at either end of the Recordset. If your provider supports them, bookmarks can be used to mark your place before using the Find method so you can return to your location. A bookmark is a Variant type value that uniquely identifies a record in a Recordset object.

You can also use a variant array of bookmarks with the Recordset Filter method to filter on a selected set of records. For details about this technique, see Filtering the Results in the topic, Working with Recordsets, later in this chapter.

You can use the Bookmark property to get a bookmark for a record, or set the current record in a Recordset object to the record identified by a valid bookmark. The following code uses the Bookmark property to set a bookmark and then return to the bookmarked record after moving on to other records. To determine if your Recordset supports bookmarks, use the Supports method.

 
'BeginBookmarkEg 
 Dim varBookmark As Variant 
 Dim blnCanBkmrk As Boolean 
 
 objRs.Open strSQL, strConnStr, adOpenStatic, adLockOptimistic, adCmdText 
 
 If objRs.RecordCount > 4 Then 
 objRs.Move 4 ' move to the fifth record 
 blnCanBkmrk = objRs.Supports(adBookmark) 
 If blnCanBkmrk = True Then 
 varBookmark = objRs.Bookmark ' record the bookmark 
 objRs.MoveLast ' move to a different record 
 objRs.Bookmark = varBookmark ' return to the bookmarked (sixth) record 
 End If 
 End If 
'EndBookmarkEg 

The Supports method is covered in more detail later.

Except for the case of cloned Recordsets, bookmarks are unique to the Recordset in which they were created, even if the same command is used. This means that you cannot use a Bookmark obtained from one Recordset to move to the same record in a second Recordset opened with the same command.