Bookmark Property Example

This example uses the Bookmark property to let the user flag a record in a Recordset and return to it later.

Sub BookmarkX()

	Dim rstPublishers As ADODB.Recordset
	Dim strCnn As String
	Dim avarPosition() As Variant
	Dim intSetIndex As Integer
	Dim intGetIndex As Integer
	Dim strMessage As String
	Dim intCommand As Integer
	Dim varBookmark As Variant

	' Open recordset with data from Publishers table.
	strCnn = "driver={SQL Server};server=srv;" & _
		"uid=sa;pwd=;database=pubs"
	Set rstPublishers = New ADODB.Recordset
	rstPublishers.CursorType = adOpenStatic
	rstPublishers.Open "SELECT pub_id, pub_name FROM publishers " & _
		"ORDER BY pub_name", strCnn

	With rstPublishers
		' Store record position.
		.MoveFirst
		ReDim avarPosition(.RecordCount)
		intSetIndex = 1
		Do Until .EOF
			avarPosition(intSetIndex) = !pub_id
			intSetIndex = intSetIndex + 1
			.MoveNext
		Loop
		
		.MoveFirst

		Do While True
			' Get the current record position, then display information
			' about current record and get user input.
			For intGetIndex = 1 To UBound(avarPosition)
				If avarPosition(intGetIndex) = !pub_id Then Exit For
			Next
			strMessage = "Publisher: " & !pub_name & _
				vbCr & "(record " & intGetIndex & _
				" of " & .RecordCount & ")" & vbCr & vbCr & _
				"Enter command:" & vbCr & _
				"[1 - next / 2 - previous /" & vbCr & _
				"3 - set bookmark / 4 - go to bookmark]"
			intCommand = Val(InputBox(strMessage))

			Select Case intCommand
				' Move forward or backward, trapping for BOF
				' or EOF.
				Case 1
					.MoveNext
					If .EOF Then .MoveLast
				Case 2
					.MovePrevious
					If .BOF Then .MoveFirst

				' Store the bookmark of the current record.
				Case 3
					varBookmark = .Bookmark

				' Go to the record indicated by the stored
				' bookmark.
				Case 4
					If IsEmpty(varBookmark) Then
						MsgBox "No Bookmark set!"
					Else
						.Bookmark = varBookmark
					End If

				Case Else
					Exit Do
			End Select

		Loop

		.Close
	End With

End Sub

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.