Switchboard forms are a common way to facilitate navigation in an application. It is common for switchboard forms to contain several command buttons that users can click to open another form. This section offers two approaches to using switchboard forms: one based on hyperlinks and the other based on VBA procedures.
Hyperlink navigation is particularly easy to create 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 World Wide Web pages on the Internet or pages on an intranet. Access lets you use hyperlinks with 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 not only easier but can yield faster-loading forms because forms with hyperlinks created in Design view do not require that a module contain VBA code. To deliver this benefit, a form must have its Has Module 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 13 for coverage of using hyperlinks for Web development.
Figure 5-2 shows four forms that use 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 on the main form. Once another form has the focus, the user can return the focus to the switchboard by clicking the hyperlink to the main form.
Figure 5-2. A simple hyperlink navigation system.
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:
NOTE
By default, unfollowed hyperlinks in an Access session are blue and followed hyperlinks are maroon. You can change these colors by choosing Options from the Tools menu, clicking on the General tab, and then clicking Web Options. Use the two list boxes in the Web Options dialog box to set the colors.
Figure 5-3 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.
Figure 5-3. The Insert Hyperlink dialog box for the label on the frmGoHere form.
Another common way to implement switchboard navigation is with VBA code behind command button click events. This approach offers richer exposure to Access functionality than hyperlink-based navigation because you can mix navigation functions with other events, such as the closing of a form. (With a hyperlink, one form can open another, but returning to a switchboard leaves the child form open; VBA-based navigation lets an application programmatically close the child form when a user returns to the main form.) 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-4 shows a pair of forms that use the VBA approach to navigation. This sample relies on 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.)
Figure 5-4. These forms use code behind button click events to perform navigation.
The following pair of event procedures shows the code for the click events of the two buttons in Figure 5-4. 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 stDocName As String stDocName = "frmButtonGoHere" DoCmd.OpenForm stDocName 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 stDocName As String Dim stLinkCriteria As String stDocName = "frmButtonSwitchboard" DoCmd.OpenForm stDocName stDocName = "frmButtonGoHere" DoCmd.Close acForm, stDocName, acSaveNo Exit_cmdReturnToMain_Click: Exit Sub Err_cmdReturnToMain_Click: MsgBox Err.Description Resume Exit_cmdReturnToMain_Click End Sub |