OnAction Property

Applies To

CommandBarButton object, CommandBarComboBox object, CommandBarControl object, CommandBarPopup object.

Description

Returns or sets the name of the Visual Basic macro that will be run when the user clicks or changes the value of a command bar control. Read/write String.

Note The container application determines whether the value is a valid macro name.

Specifics (Microsoft Access)

In Microsoft Access 97, you can use the Toolbar Control Properties dialog box for command bar controls to set the OnAction property for a control on a command bar. Display the Customize dialog box by pointing to Toolbars on the View menu and clicking Customize. For menu bar and toolbar controls, display the menu bar or toolbar, and then right-click the control whose OnAction property you want to set. For shortcut menu controls, select the Shortcut Menus check box on the Toolbars tab of the Customize dialog box, then find the shortcut menu control you want on the menu bar that's displayed and right-click the control. Click the Properties command. Enter the macro or function you want to run in the On Action box.

You can use the OnAction property only to run a macro or a function. To run a macro, select it from the drop-down list in the On Action box, or enter its name in the box. To run a function, enter its name in the form =functionname( ) in the box.

You can also set the OnAction property to an expression. The expression can contain a built-in or user-defined function or combination of functions. When Microsoft Access evaluates an expression, it performs all of the operations in the expression. For example, the following is an expression that runs the user-defined function "CustomControlFunction":

=CustomControlFunction()
In Visual Basic, the OnAction property takes a string, so to set it to an expression, you must pass it a string that contains the expression. In passing the string, be careful to enclose strings within the string in quotation marks ("). (Note that in the On Action box in the Toolbar Control Properties dialog box, an enclosed string in the expression should only have one set of quotation marks around it.) Also, make sure you include the equal sign (=) to specify that the string contains an expression. If your function takes no arguments, remember to include parentheses [( )] within the string. You can set the OnAction property to the previous expression with the following string:

"=CustomControlFunction()"
Note that you must set the OnAction property to the name of a Function procedure; you can't set it to the name of a Sub procedure.

For custom command bar controls, you must set this property in order for the control to perform any action. If you set this property for a built-in control, the OnAction property setting overrides the normal actions that the control performs; that is, you can force a built-in control to perform different actions from its default actions. The control becomes in effect a custom control.

Example

This example adds a command bar control to the command bar named "Custom" and sets the macro named "MySub" to run whenever the button is clicked.

Set myBar = .CommandBars("Custom")
Set myControl = myBar.Controls.Add(Type:=msocontrolButton)
With myControl
    .FaceId = 2
    .OnAction = "MySub"
End With
myBar.Visible = True