Command BarButton Object

The CommandBarButton object is a specialized command bar control that represents a button on a toolbar or menu that the user can click. You can use the CommandBarButton object in conjunction with the CommandBar object to build your own custom toolbars, which is demonstrated in the next procedure.

Private Sub cmdAddCustomCommandBar_Click()

Dim customBar As CommandBar

Dim newButton As CommandBarButton

Set customBar = CommandBars.Add("Sheila")

Set newButton = customBar.Controls _

.Add(msoControlButton, CommandBars("Insert") _ .Controls("Table").ID)

Set newButton = customBar.Controls _

.Add(msoControlButton, CommandBars("Insert") _ .Controls("Query").ID)

Set newButton = customBar.Controls _

.Add(msoControlButton, CommandBars("Insert") _ .Controls("Form").ID)

customBar.Visible = True

End Sub

Using the Add method of the CommandBar object, I can create a new custom command bar called MyCommandBar. Then I can use Add methods of new CommandBarButton objects to create buttons on my custom command bar. Note that the Controls property of the CommandBar object returns a CommandBarsControls collection. I use this collection's Add method to add a new button to the command bar. The Add method takes five optional parameters. In my example, the first parameter determines the type of control to be added. The options for this parameter are MsoControlType constants:

msoControlButton msoControlEdit msoControlDropdown msoControlComboBox msoControlPopup

The next parameter in the Add method requires an Integer value, which represents a built-in control. In my example, I use the ID property of the Controls collection to return an Integer value representing a known control.

You can pass a string into the Controls collection, which identifies a specific control name. The control name, however, must match a control name found in the CommandBars collection. Note that control and command bar names are found in Access command bars, not in VBE command bars.

Once a custom command bar has been created, it is added to your current application. To manage the newly added custom command bar, simply right-click a toolbar and select Customize or select Customize from the View/Toolbars menu group. Custom command bars can be removed from the same Customize Toolbars window.

0 0

Post a comment