>
rstCustomers.Bookmark = rstCustomers.BookmarkThe value of the Bookmark property isn't the same as a record number. Note The Bookmark property doesn't apply to forward-only scrolling snapshots. See Also Bookmarkable Property, RecordCount Property. Specifics (Microsoft Access) When you write Visual Basic® code that uses the Bookmark property, you must include an Option Compare Binary statement in the Declarations section of the module. The Bookmark property sets and returns a bookmark, which is a Variant array of Byte data. The string comparison method for the module must therefore be binary. If a bookmark is evaluated with a text-based string comparison method, such as the Option Compare Text statement or the default setting for the Option Compare Database statement, the current record may be set to an incorrect record. Note Don't confuse this property with the Microsoft Access Bookmark property, which applies to a Form object and stores a bookmark for a particular record in the table or query underlying the form. These two properties do not interfere with each other; you can have separate bookmarks on a form and on a Recordset object at the same time. Example This example shows how you can save your place in a Recordset by saving a bookmark. Once saved, this bookmark can be applied to the Bookmark property to reposition the current record pointer to any location in the Recordset. The FindYear function works with the Biblio.mdb database. It looks up all titles in a given Recordset published in a given year, and fills a string array with the bookmarks of any records that match.
Function FindYear (intYear As Integer, rstBooks As Recordset, _
avarRecord() As Variant) As Integer Dim intCount As Integer rstBooks.FindFirst "[Year Published] = " & intYear
If (rstBooks.NoMatch = True) Or (rstBooks.Bookmarkable = False) _Example (Microsoft Access) The following example moves through the records of the Employees table from the beginning of the file to the end and stores the value of the Bookmark property for each record in an array.
Then FindYear = 0 Exit Function Else Do Until rstBooks.NoMatch = True avarRecord(intCount) = rstBooks.Bookmark rstBooks.FindNext "[Year Published] = " & intYear intCount = intCount + 1 Loop End If FindYear = intCount End Function
' Include this statement in the Declarations section of the module. Option Compare Binary Sub RecordPositions() Dim dbs As Database, rst As Recordset, fld As Field Dim intI As Integer ' Declare array to hold bookmarks. Dim varRecord() As Variant ' Return Database variable that points to current database. Set dbs = CurrentDb ' Open a table-type Recordset object. Set rst = dbs.OpenRecordset("Employees") Set fld = rst.Fields!LastName ' Populate the Recordset object. rst.MoveLast rst.MoveFirst ' Redimension array with value of RecordCount as upper bound. ReDim varRecord(0 To rst.RecordCount - 1) intI = 0 ' Check Bookmarkable property of Recordset object. If rst.Bookmarkable Then Do Until rst.EOF ' Populate array with bookmarks. varRecord(intI) = rst.Bookmark ' Increment counter. intI = intI + 1 rst.MoveNext Loop End If End SubExample (Microsoft Excel) This example prompts the user for a two-letter abbreviation for a state. The example uses this value to find up to 101 matching records in the Customer recordset in the NWINDEX.MDB database. It then marks each record with a bookmark and copies the values of the first and third fields onto Sheet1. To create the NWINDEX.MDB database, run the Microsoft Excel example for the CreateDatabase method.
Dim Found(100) i = 0 Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB") Set rs = db.OpenRecordset("Customer") Sheets("Sheet1").Activate region = Application.InputBox("What state do you want data from?", _ "Specify two letters (e.g. 'WA')", Type:=2) If region = False Then ' user cancelled InputBox Exit Sub End If criteria = "[REGION] = '" & region & "'" rs.FindFirst criteria If rs.NoMatch Or rs.Bookmarkable = False Then MsgBox "No records for this state" Exit Sub Else Do Until rs.NoMatch = True i = i + 1 Found(i) = rs.Bookmark rs.FindNext criteria Loop End If For n = 1 To i rs.Bookmark = Found(n) Cells(n + 1, 1).Value = rs.fields(0).Value Cells(n + 1, 2).Value = rs.fields(2).Value Next MsgBox "There are " & i & " records from this region" rs.Close db.Close