Access Cookbook - Ken Getz [187]
The actual code that implements the mark-and-return facility is small, and basically revolves around grabbing the form's Bookmark property and storing it between calls to the acbHandleMarkReturn function. This is handled by the Select Case statement in acbHandleMarkReturn:
Public Function acbHandleMarkReturn(msAction As MarkState)
Static svarPlaceHolder As Variant
Select Case msAction
Case msMark
' Mark record position
svarPlaceHolder = Me.Bookmark
Me.tglMark.Caption = "Return to Saved Place"
Case msReturn
' Return to marked position
Me.Bookmark = svarPlaceHolder
svarPlaceHolder = Empty
Me.tglMark.Caption = "Save Place"
Case msDiscard
' Reset marked position
' and unpress button
svarPlaceHolder = Empty
Me.tglMark.Caption = "Save Place"
Me.tglMark.Value = False
Case Else
' Shouldn't happen
MsgBox "Unexpected value for intAction", _
vbCritical + vbOKOnly, "acbHandleMarkReturn"
End Select
End FunctionMe.
The msMark enumerated value case is executed when the user depresses the toggle button, so the code stores away the bookmark in the svarPlaceHolder static variable and changes the caption to indicate the new state of the button. Notice that we used a static variable rather than a module-level global variable. A static variable is a better choice in this situation because we are changing the value of the variable only within this one function.
When called with the msReturn value, the code sets the form's bookmark to the previously stored value, clears svarPlaceHolder, and resets the caption to the default.
Finally, when called with the msDiscard constant value, the code clears svarPlaceHolder, resets the caption, and sets the Value property of the toggle button control to False. This causes the toggle button to reset itself to the unpressed state, which is necessary because the function was called from the shortcut menu macro without toggling the button.
We made the acbHandleMarkReturn function public because we needed to call it from the shortcut menu. However, you can only call public functions that are in standard modules from toolbar buttons or menu items, which is why we needed the additional acbAbandonBookmark function to call the function that is in the form. Note the syntax that acbAbandonBookmark uses to call the public acbHandleMarkReturn function in the form, passing in the value msDiscard to specify that the bookmark should be abandoned:
Public Function acbAbandonBookmark( )
Call Form_frmCustomer.acbHandleMarkReturn(msDiscard)
End Function
An alternate way to offer this functionality—the ability to browse other records and return to a previous record—is to create multiple instances of the same form. This method was demonstrated in the Solution in Recipe 2.11.
BOOKMARKS
A bookmark is an array of bytes that points to the current record of an open recordset (or in the case of the form's bookmark, the current record of a form's recordset). Bookmarks make sense only within the lifetime of the currently open recordset (or form). If you requery or close and rerun the query or form, the set of bookmarks will be different. A bookmark is not a record number; it's a dynamically created handle (or pointer) to the current record. To store bookmarks, you can use a variable of type Variant.
9.5. Carry Data Forward from Record to Record
Problem
You'd like to reduce the tedium of data entry by carrying forward selected values from one record to the next. Ideally, this feature will be user-selectable at runtime so that each user can indicate, on a control-by-control basis, whether the current value of a control should carry forward onto newly added records. Is there any way to implement this in Access?
Solution
There are two parts to this problem: the mechanics of carrying a value from one record to the next, and how best to let a user