You can create custom menus to display with your forms and reports; there's no limit as to how many you can use. You can attach each menu to one or more forms or reports. Quite often, you will want to restrict what users can do while they're working with a form or report. By creating a custom menu, you can restrict and customize what users are allowed to do.
Prior to Access 97, Access users could create a custom menu bar by setting the MenuBar property to the name of a menu bar macro. This function was supported for backward compatibility only. In Access 97, and all subsequent versions of Access, custom menu bars, toolbars, and pop-up menus are all referred to as
command bars . To create any of these three objects, choose View, Toolbars and then select Customize. After you have created a custom menu bar, toolbar, or pop-up menu, you can easily associate it with forms and reports by using the Menubar, Toolbar, and Shortcut Menubar properties, respectively.
Follow these steps to create a custom menu bar:
Choose View, Toolbars and click Customize, or right-click any command bar and select Customize.
When the Customize dialog box opens, click the Toolbars tab and then click New (see Figure 9.14).
Assign a name to the new menu bar, toolbar, or pop-up menu. The new command bar then appears.
Click the Properties button on the Customize dialog box to view the properties for your newly created command bar. In the Toolbar Properties dialog box, you name the toolbar, select the toolbar type, indicate the type of docking that's allowed, and set other options for the command bar. The Type drop-down list allows you to select Menu Bar, Toolbar, or Pop-up. The Docking options are Allow Any, Can't Change, No Vertical, and No Horizontal. You can also choose whether the user will be allowed to customize or move the command bar.
Select the options you want and click Close.
NOTE
We refer to menu bars, toolbars, and pop-up menus generically as command bars. The process to create each type of object is very similar. You simply use the Type property of the command bar to designate the type of object you want to create.
Now you're ready to add items to the new command bar. The process differs slightly, depending on whether you selected a toolbar, menu bar, or pop-up menu. To add items to a command bar, click the Commands tab of the Customize dialog box, shown in Figure 9.15, and drag and drop command icons onto your new command bar.
Here are some tips to help you to create custom menu bars, toolbars, and pop-up menus:
Menu items can contain text only or images and text. To select one of these options, right-click a menu item and select Default Style, Text Only (Always), Text Only (in Menus), or Image and Text. To customize an image, right-click a menu item and select Change Button Image. Choose one of the available images. To modify the button image, right-click a menu item and select Edit Button Image; this opens the Button Editor dialog box (see Figure 9.16). If you want to reset the button to its original image, right-click the menu item and select Reset Button Image.
If you want to modify several properties of a menu item at once, you can right-click the menu item and select Properties to open the File Control Properties dialog box (see Figure 9.17). Here you can select attributes for the menu item, such as the Caption, Screen Tip, Style, Help File, and Help ContextID. You can also associate an action with a custom menu item (covered in the next section).
In Access, it's easy to customize your menus with both built-in commands and custom-built functions. For built-in commands, you can simply drag and drop commands onto your command bars. To have a command bar item run a custom-built function, you need to create a custom item and set its properties, as explained in the following steps:
Select the File category from the Categories list box in the Customize dialog box.
Click and drag the Custom option from the Commands list box to the position you want for the menu.
Right-click the new menu item and select Properties.
Type the name of the function or subroutine you want to call in the On Action drop-down list. If the procedure you're calling is a function, you must precede the function name with an equal sign (=) and include any parameters in parentheses following the function name.
Click Close to close the Control Properties dialog box.
Click Close to close the Customize dialog box.
You can also use the Customize dialog box to delete and rename menus by following these steps:
Right-click any command bar and select Customize.
Click in the Toolbars list box to select the command bar you want to delete or rename.
Click Delete to delete the command bar, or Rename to rename it.
Sub CreateCustomCommandBar() Dim cbr As CommandBar Dim btn As CommandBarButton 'Attempt to point the command bar object at 'a command button named My Command Bar Set cbr = CommandBars("My Command Bar") 'If an error occurs, the command bar doesn't exist 'so create it If Err.Number Then Set cbr = CommandBars _ .Add(Name:="My Command Bar", Position:=msoBarTop) End If 'Attempt to add a button "Are You Sure?" Set btn = cbr.Controls("Are You Sure?") 'If an error occurs, the custom button doesn't exist 'so create it If Err.Number Then Set btn = cbr.Controls.Add(msoControlButton, , , , True) End If 'Set properties of the button With btn .Caption = "Are You Sure?" .BeginGroup = True .OnAction = "MessageBoxAnswer" .Style = msoButtonCaption End With End Sub
This code illustrates that, by using the VBA language, you have full control over command bar objects. It begins by creating CommandBar and CommandBarButton object variables; then it sets the CommandBar object variable to a command bar called My Command Bar. If this causes an error, you know that the My Command Bar command bar doesn't exist. The Add method is used to add the command bar, which will be placed at the top of the screen. The routine then tries to point at a command bar button called Are You Sure?. If this causes an error, the Add method of the Controls collection of the CommandBar object is used to add a command button to the collection. The button's caption is set to Are You Sure?, a group is added, and the command button's action is set to call the subroutine MessageBoxAnswer. The command button's style is set to display just a caption.