[ Team LiB ] |
Recipe 9.4 Mark a Record on a Form and Return to It Later9.4.1 ProblemSometimes you are interrupted when you're editing a record on a form and need to move quickly to some other record. You'd like a way to save your place and easily return to it later. Is there an easy way to do this in Access? 9.4.2 SolutionAccess forms have a Bookmark property that is similar to the bookmark you use when you put a book down but want to be able to quickly return to where you left off. This solution shows how to use VBA code to store the bookmark value of a particular record and return to it, presenting this functionality to your users with a toggle button. The solution also shows you how to add a custom shortcut menu to a control. Follow these steps to add the ability to return to a designated record in your own forms:
Figure 9-11. Creating a shortcut menu from the Toolbars tab of the Customize dialog
Figure 9-12. Adding a custom item to a shortcut menu
To see how this works, load the 09-04.MDB database and open the frmCustomer form, which contains 500 customer records. Navigate to a record and begin to make a change to it. For example, in Figure 9-13, we made some edits to Margaret Woods's record before marking it. Click on the Save Place toggle button in the form's header to mark the current record and save your place in the recordset. The toggle button will remain depressed and its caption will change to Return to Saved Place (see Figure 9-14). Now navigate to some other record. Click on the toggle button again, and you will return instantly to the earlier "marked" record. Figure 9-13. The frmCustomer form before marking the current recordFigure 9-14. The frmCustomer form after marking the current recordMark the record again and navigate to yet another record. Perhaps this time you have changed your mind and wish to abandon the earlier marked record in favor of the current one. However, if you press the toggle button a second time, you will return to the previously marked record, losing your new place. You can remedy this situation by right-clicking while the mouse cursor is over the toggle button control. A shortcut menu giving you the option to abandon the previously marked record will appear (see Figure 9-15). Select this option, and you'll now be able to mark the current record instead. Figure 9-15. The toggle button's shortcut (right-click) menu9.4.3 DiscussionThe mark-and-return facility built into the frmCustomer form has several interesting user interface aspects. First, the toggle button is the main user interface element. This control type is ideally suited for this situation because it is able to store binary state information that visually matches the two states you wish to represent (mark and return). Second, the shortcut menu, although a little less easily discovered than the toggle button, allows you to offer the extra "abandon" functionality without taking up a lot of screen space. 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.
|
[ Team LiB ] |