Add Method (CommandBarControls Collection)

Applies To

CommandBarControls collection object.

Description

Creates a new command bar control and adds it to the collection of controls on the specified command bar. Returns a CommandBarButton, CommandBarComboBox, or CommandBarPopup object.

Syntax

expression.Add(Type, Id, Parameter, Before, Temporary)

expression Required. An expression that returns a CommandBarControls object.

Type Optional Variant. The type of control to be added to the specified command bar. Can be one of the following MsoControlType constants: msoControlButton, msoControlEdit, msoControlDropdown, msoControlComboBox, or msoControlPopup.

Id Optional Variant. An integer that specifies a built-in control. If the value of this argument is 1, or if this argument is omitted, a blank custom control of the specified type will be added to the command bar.

Parameter Optional Variant. For built-in controls, this argument is used by the container application to run the command. For custom controls, you can use this argument to send information to Visual Basic procedures, or you can use it to store information about the control (similar to a second Tag property value).

Before Optional Variant. A number that indicates the position of the new control on the command bar. The new control will be inserted before the control at this position. If this argument is omitted, the control is added at the end of the specified command bar.

Temporary Optional Variant. True to make the new control temporary. Temporary controls are automatically deleted when the container application is closed. The default value is False.

See Also

CustomizationContext property.

Specifics (Microsoft Access)

You can set the Id argument of this method to an Integer value that corresponds to a built-in command button or menu item. This will cause the new control to have the appearance and functionality of the existing command button or menu item.

However, if you set the Id argument to the Integer value of a Microsoft Office command button or menu item that has been modified by Microsoft Access (for example, Microsoft Access uses a different label, different shortcut text, or uses text instead of an icon), you won't see the Microsoft Access modifications to the command button or menu item. If you want to make sure a control you're adding to a command bar looks and functions exactly like the Microsoft Access version of the control, use the Copy method to copy the control to the command bar.

Example

This example adds a button control with a charting face to the command bar named "Custom," and then it assigns the control the procedure named "Analyze."

Set custBar = CommandBars("Custom")
custBar.Visible = True
Set graphBtn = custBar.Controls.Add(Type:=msoControlButton)
graphBtn.FaceId = 17
graphBtn.OnAction = "Analyze"