You can use the RunCode macro action in Access desktop databases to call a Visual Basic for Applications (VBA) Function procedure.

Setting

The RunCode macro action has the following argument.

Action argument

Description

Function Name

The name of the VBA Function procedure to call. Enclose any function arguments in parentheses. Enter the function name in the Function Name box in the macro design window. This is a required argument.

Note: In an Access desktop database (.mdb or .accdb), click the Build button to use the Expression Builder to select a function for this argument. Click the desired function in the list in the Expression Builder.

Remarks

The user-defined Function procedures are stored in Access modules.

You must include parentheses, even if the Function procedure doesn't have any arguments, as in the following example:

TestFunction()

Unlike user-defined function names used for event property settings, the function name in the Function Name argument doesn't begin with an equal sign (=).

Access ignores the return value of the function.

Note: You can't call a Function procedure from a macro if the function name is the same as the module name.

Tip

To run a Sub procedure or event procedure written in Visual Basic, create a Function procedure that calls the Sub procedure or event procedure. Then use the RunCode macro action to run the Function procedure.

If you use the RunCode macro action to call a function, Access looks for the function with the name specified by the Function Name argument in the standard modules for the database. However, when this action runs in response to clicking a menu command on a form or report or in response to an event on a form or report, Access first looks for the function in the form's or report's class module and then in the standard modules. Access doesn't search the class modules that appear in the Modules area of the Navigation Pane for the function specified by the Function Name argument.

This action isn't available in a VBA module. Instead, run the desired Function procedure directly in VBA.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.