Program Toolbars and Menu Bars
Microsoft Access includes command bars, which are programmable toolbars and menu bars. Using command bars, you can create custom toolbars and menus for your application.
In order to program with command bars, you must set a reference to the Microsoft Office object library. Click References on the Tools menu while in module Design view, and select the check box next to Microsoft Office Object Library.
The CommandBars collection includes all the command bars that currently exist within the application. You can add a new CommandBar object to the CommandBars collection by using the Add method of the CommandBars collection. For example, the following code creates a new command bar named MyCommandBar. Note that you need to set the new command bar's Visible property to True in order to see it.
Dim cmb As CommandBar
Set cmb = Application.CommandBars.Add("MyCommandBar")
cmb.Visible = True
Each CommandBar object has a CommandBarControls collection, which contains all the controls on the command bar. Command bar controls are different from the controls on a form. You can create different types of command bar controls, including buttons, combo boxes, and pop-ups. You can combine these controls to create a custom toolbar or menu bar.
To refer to the CommandBarControls collection, use the Controls property of the CommandBar object. To add a control to a command bar, use the Add method of the CommandBarControls collection, specifying which type of control you want to create. The following example adds a new button to the command bar created in the previous example:
Dim cbc As CommandBarControl
Set cbc = cmb.Controls.Add(msoControlButton)
cbc.Caption = "Button1"
cbc.Style = msoButtonCaption
You can specify an expression to evaluate or a macro to run when the user clicks a command bar control. Set the OnAction to the name of a macro or to an expression that contains a built-in or user-defined function. For example, the following line of code sets the OnAction property of a CommandBarControl object to an expression that includes the MsgBox function. This example uses the CommandBar and CommandBarControl objects created in the previous two examples:
CommandBars("MyCommandBar").Controls("Button1").OnAction = "=MsgBox(""Wow!"")"
Note Unlike most other collections in Microsoft Access, the CommandBars collection and all the collections it contains are indexed beginning with 1 rather than 0.