Switchboard Forms


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.

Navigating with Hyperlinks

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.

click to expand
Figure 5.4: A simple hyperlink form 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:

  1. Click the Build button () next to the HyperlinkAddress property or the HyperlinkSubAddress property on the label's property sheet.

  2. 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.

  3. Enter the appropriate information for the type of object you selected.

  4. Click OK.

    Note  

    By default, unfollowed hyperlinks in an Access session are blue and followed hyperlinks are violet . 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. The Web Options dialog box also offers a check box that lets you set whether hyperlinks are underlined . Any settings that you make in the Web Options dialog box will affect new hyperlinks that you create but not previously created hyperlinks.

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.

click to expand
Figure 5.5: The Insert Hyperlink dialog box for the label on the frmGoHere form.

Navigating with Code Behind 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.

click to expand
Figure 5.6: These forms use code behind button Click events to perform navigation.

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.

 PrivateSubcmdGoHere_Click()  OnErrorGoToErr_cmdGoHere_Click     DimstrDocNameAsString strDocName= "frmButtonGoHere" DoCmd.OpenFormstrDocName DoCmd.CloseacForm, "frmButtonSwitchboard"     Exit_cmdGoHere_Click: ExitSub     Err_cmdGoHere_Click: MsgBoxErr.Description ResumeExit_cmdGoHere_Click EndSub     PrivateSubcmdReturnToMain_Click()  OnErrorGoToErr_cmdReturnToMain_Click     DimstrDocNameAsString DimstrLinkCriteriaAsString     strDocName= "frmButtonSwitchboard" DoCmd.OpenFormstrDocName DoCmd.CloseacForm, "frmButtonGoHere",acSaveNo Exit_cmdReturnToMain_Click: ExitSub     Err_cmdReturnToMain_Click: MsgBoxErr.Description ResumeExit_cmdReturnToMain_Click EndSub 
start sidebar
Running Procedures from Hyperlinks

A drawback of the navigation system we just examined is that the form stays open after the hyperlink transfers the focus from the current form to another location. Recall that one key advantage of a hyperlink navigation system is that your forms do not require modules behind them. This absence robs your application of the traditional place where code resides to close forms automatically.

You can still automatically remove forms from the screen when using hyperlink navigation. The trick is to use a function procedure in a standalone module or a macro. (You cannot use a subprocedure.) Many different hyperlinks can call the same function procedure. The argument that the link passes to the function procedure tells the procedure which form to close. You can invoke the function procedure from the On Click event setting of a hyperlink's label control. The event procedure fires before the hyperlink action transfers the focus.

Figure 5-7 shows a label control selected with a hyperlink setting. The property sheet below the form shows the expression for invoking the CloseNamedForm function procedure from the Click event of the hyperlink. Notice that the name of a form appears as an argument in the function procedure call. This design lets multiple hyperlinks from di



Programming Microsoft Office Access 2003
Programming MicrosoftВ® Office Access 2003 (Core Reference) (Pro-Developer)
ISBN: 0735619425
EAN: 2147483647
Year: 2006
Pages: 144
Authors: Rick Dobson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net