VBA & Macro Problems

Run a Public VBA Sub from an Access Command Button

Bushra ParveenBushra Parveen Nov 25, 2026 872 views

Question details

The user wants to run a public VBA Sub when an Access command button is clicked.

How to Run a Public VBA Sub from an Access Command Button
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.
Before you start

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.

Solution 1Recommended

Call the Public Sub from the Button's Click Event

Use an event procedure when you want to keep the existing public Sub.

1
Open the button properties

In Access, open the form in Design View, select the command button, and open its Property Sheet.

2
Create a click event

On the Event tab, set On Click to [Event Procedure], click the ellipsis button, and choose Code Builder if prompted.

3
Call the Sub

Inside the generated button Click procedure, enter Call DoThisThing, replacing the name with your Sub's name and supplying any required arguments.

4
Test the button

Save the form, switch to Form View, and click the command button.

Call the Public Sub from the Button's Click Event
Check procedure visibility: A public Sub in a standard module is generally accessible from the form's event procedure. If it belongs to another form or class module, it must be called through an appropriate object reference.
Free Microsoft Office alternative

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.

Create and edit documents, spreadsheets, and presentations in one suiteWork with Microsoft Office formats including DOCX, XLSX, and PPTXUse a familiar interface for a straightforward transitionHandle office documents alongside Access database workflows
microsoft office alternative - wps office

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.