Switchboard forms are a common way to facilitate navigation among other forms in an application. Switchboard forms typically contain several command buttons that users can click to open another form. This section offers three approaches for implementing switchboard forms: using hyperlinks without any associated code, invoking function procedures in a standalone code module from hyperlinks on a form, and using VBA procedures in a module behind a form.
Hyperlink navigation is particularly easy to construct because you do not have to write any code (although you can manage hyperlinks with VBA). Hyperlinks can act as shortcuts to database objects in an application, documents on your hard drive, files on a network, or Web pages on the Internet or an intranet. Access lets you assign hyperlinks to labels, buttons, and images.
You can set and edit hyperlink properties from a form's Design view or programmatically using VBA. Using the manual procedures for setting and editing hyperlinks is easier and can yield faster-loading forms because forms with hyperlinks created in Design view do not require a module containing VBA code. To deliver this benefit, a form must have its HasModule property set to No.
Note |
The Hyperlink data type lets an Access application launch hyperlinks from table or query fields. While hyperlink fields are in many ways like the hyperlink properties, you use them differently. See Chapter 14 for coverage of using hyperlinks for Web development. |
Figure 5-4 shows four forms that demonstrate the operation of a simple navigation system. The main switchboard form on the left transfers focus to one of the other three forms when the user clicks a hyperlink. Once another form has the focus, the user can return the focus to the switchboard form by clicking the hyperlink to it.
You can easily introduce more than two tiers into a navigation system. Typically, each child form can return focus to its parent form. Main forms often include a way to exit the application or exit Access.
To create a hyperlink using the Label control, follow these steps:
Click the Build button (…) next to the HyperlinkAddress property or the HyperlinkSubAddress property on the label's property sheet.
In the Insert Hyperlink dialog box, select the type of object to link to from the Link To list on the left edge of the dialog box.
Enter the appropriate information for the type of object you selected.
Click OK.
Figure 5-5 shows the Insert Hyperlink dialog box for the label on the frmGoHere form in this chapter's sample database. The hyperlink simply transfers focus back to the switchboard form.
Another common way to manage switchboard navigation is with VBA code from Click events for command buttons. Because you can mix navigation functions with other events, such as the closing of a form, this approach offers richer exposure to Access functionality than hyperlink-based navigation without calls to function procedures.
An event procedure also gives you greater control over multiple database objects. Hyperlink-based navigation merely transfers focus to another object, such as a form.
Figure 5-6 shows a pair of forms that use the VBA approach to navigation. This sample relies on command button Click events. When the user clicks one of the switchboard buttons, the code for the button's Click event opens the target form and closes the main switchboard. Clicking the Return To Main button on the target form closes the form and opens the main switchboard form. (The sample uses command buttons, but you can use any other type of control that lets a user generate events.)
The following pair of event procedures shows the code for the Click events of the two buttons in Figure 5-6. The first procedure switches control from the main switchboard form to the second form. It also closes the main form. The second procedure transfers control back to the main switchboard and then closes the second form. I used the Command Button Wizard to create a first draft of each procedure; I then added a line of code to each procedure to close the appropriate form.
Private Sub cmdGoHere_Click() On Error GoTo Err_cmdGoHere_Click Dim strDocName As String strDocName = "frmButtonGoHere" DoCmd.OpenForm strDocName DoCmd.Close acForm, "frmButtonSwitchboard" Exit_cmdGoHere_Click: Exit Sub Err_cmdGoHere_Click: MsgBox Err.Description Resume Exit_cmdGoHere_Click End Sub Private Sub cmdReturnToMain_Click() On Error GoTo Err_cmdReturnToMain_Click Dim strDocName As String Dim strLinkCriteria As String strDocName = "frmButtonSwitchboard" DoCmd.OpenForm strDocName DoCmd.Close acForm, "frmButtonGoHere", acSaveNo Exit_cmdReturnToMain_Click: Exit Sub Err_cmdReturnToMain_Click: MsgBox Err.Description Resume Exit_cmdReturnToMain_Click End Sub