Adding Custom Menus, Toolbars, and Shortcut Menus to Your Forms
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.
Designing a Menu
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:
Figure 9.14. Using the Customize dialog box to create a new command bar.

NOTEWe 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.
Figure 9.15. Use the Commands tab to add items to a 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.
Figure 9.16. Modifying or creating button images with the Button Editor.
- 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).
Figure 9.17. Modifying menu item properties with the File Control Properties dialog box.
Associating a Command with a Menu Item
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:
Deleting and Renaming Menus
You can also use the Customize dialog box to delete and rename menus by following these steps:
Manipulating Command Bars by Using Code
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.