Online Book Reader

Home Category

Access Cookbook - Ken Getz [183]

By Root 1914 0
must make sure to check whether the one that has closed is actually the last form. As an alternative, you can open a hidden form in your Startup form or AutoExec macro and call your cleanup processing from this form's Close event. Access will automatically close this form when the user exits, and since this was the first form opened, it will be the last form closed.

The individual calls to the acbSetAutoKeys function are attached to the forms' Activate events instead of their GotFocus events for a very good reason. Unless there are no controls on a form that can get the focus, the form itself will never receive the focus. Only forms consisting strictly of graphic objects and disabled controls will ever trigger a form-level GotFocus event.

It is interesting to note that AutoKeys functionality is just about the only thing left in Access that can be done only by using macros, not in VBA code.

9.2. Create a Form with No Menu or Toolbar


Problem


You'd like to completely disable menus for a form, and the toolbar too. Is there any way to remove menus and toolbars from a form?

Solution


If you set the MenuBar property of a form to point to a macro in Access that contains no macro actions, you can trick Access into not displaying any menus. This solution demonstrates this trick and also discusses how you can apply it to the global menus of an application. In addition, you'll learn how to use VBA code to remove a form's toolbar.

To create forms in your database without any menus, follow these steps:

Create a new macro sheet without any actions. The mcrNoMenus macro sheet in 09-02.MDB has no macro actions.

Create a new form or open an existing form in design view. Select the menu macro from Step 1 as the MenuBar property for the form.

Add the following Activate and Deactivate event procedures to the form to remove the toolbar for this form only:

Private Sub Form_Activate( )

DoCmd.ShowToolbar "Form View", acToolbarNo

End Sub

Private Sub Form_Deactivate( )

DoCmd.ShowToolbar "Form View", acToolbarWhereApprop

End Sub

Optionally, you may wish to also eliminate right-click shortcut menus for your form. To do this, set the ShortcutMenuBar property of the form to No.

Save the form.

To see an example, load the 09-02.MDB sample database. Open the frmCustomerDefaultMenus form in form view and note that the default Access menu and toolbar are available at the top of the screen (see Figure 9-4). Close this form and open frmCustomerNoMenus. Note the absence of any menu or toolbar for the form (see Figure 9-5).

Figure 9-4. The frmCustomerDefaultMenus form with the default Access menu bar

Figure 9-5. The frmCustomerNoMenus form with no menu bar

Discussion


In early versions of Access, macros were the only method of creating custom menus. Despite the newer Command Bar menus and toolbars supported in recent versions of Access, you can still create custom menus in Access by creating menu macros. When you open a form with custom menus, Access reconstructs the custom menus from the hierarchy of macros attached to the form's MenuBar property. However, if you attach an empty macro to the MenuBar property, Access creates a blank menu for the form.

The ShowToolbar macro action, which you call in VBA using DoCmd.ShowToolbar, enables you to show or hide any toolbar. The code hides the default toolbar when the form becomes active. The Deactivate code is equally important—without it, that toolbar will remain hidden for all subsequent forms that you view. The Deactivate event procedure tells Access to show that toolbar again whenever it is appropriate.

You may want to eliminate menus for a form to reduce the complexity of your application or to remove potential chinks in your application's armor. Whenever you remove built-in functionality from forms, however, you must ensure that users of your forms can still perform essential activities. For example, you wouldn't want to remove menus and set the ControlBox and CloseButton properties of your form to No unless you have added either a toolbar button

Return Main Page Previous Page Next Page

®Online Book Reader