Run a Public VBA Sub from an Access Command Button
Question details
The user wants to run a public VBA Sub when an Access command button is clicked.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A command button's On Click property is being configured to run a VBA procedure.
- Observed behavior
- An expression such as =DoThisThing() cannot directly call a public Sub.
Open the form in Design View and identify the command button and the public procedure you want to run. Confirm that the procedure is accessible from the form and note any arguments it requires.
Call the Public Sub from the Button's Click Event
Use an event procedure when you want to keep the existing public Sub.
In Access, open the form in Design View, select the command button, and open its Property Sheet.
On the Event tab, set On Click to [Event Procedure], click the ellipsis button, and choose Code Builder if prompted.
Inside the generated button Click procedure, enter Call DoThisThing, replacing the name with your Sub's name and supplying any required arguments.
Save the form, switch to Form View, and click the command button.

Use a Public Function in the On Click Expression
Use a Function when you want to call a procedure directly from the button's On Click property.
Use WPS Office for Compatible Office Documents
WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office file formats, making it easy to move everyday document work. It does not replace Microsoft Access or run Access command-button events.

Frequently Asked Questions
Why can't I use =DoThisThing() to call a Sub?
An Access event property expression calls a Function, not a Sub. Use a public Function in the expression or call the Sub from the button's [Event Procedure].
Does the Function need to return a value?
No meaningful return value is required for this use. A public Function can call the Sub and finish without assigning a result.
Will using a Function make the database noticeably slower?
A simple wrapper Function normally adds negligible overhead. The work performed by the procedure is more likely to affect performance.
What should I check if Access cannot find the Function?
Check that the Function is declared Public in a standard module, that the On Click property uses its correct name, and that any required arguments are supplied. Avoid giving the module and Function the same name.




