How to Access and Use a VBA Module Function in Microsoft Access
Question details
The user needs to correctly call and use a user-defined VBA module function without triggering availability or undefined errors.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Attempting to execute or call a custom VBA function from an Access macro, query, or form.
- Observed behavior
- The user-defined function is not available, cannot be found, or throws an error due to spelling mistakes, incorrect scope declarations, or missing VBA library references.
Before modifying your database code, carefully check your macro or query for any simple typing errors in the function name, as overlooked typos are the most common cause for this issue.
Declare the VBA Function as Public
To make a custom function accessible throughout your entire Access database, it must be explicitly declared with the Public scope inside a standard module.
By default, functions may be restricted to the module they are created in. Declaring a function as Public ensures that queries, forms, and macros across the database can interact with it.
Press Alt + F11 on your keyboard while inside Microsoft Access to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, find the module containing your function under the 'Modules' folder.
Ensure your function definition starts with the word Public. For example, change 'Function MyFunction()' to 'Public Function MyFunction() As Variant'.
Click on 'Debug' in the top menu bar, then select 'Compile [Your Database Name]'. This verifies the code and saves the changes.

Check and Add Missing VBA References
If your function relies on external libraries or is stored in a separate database file, you must establish a reference to make it accessible.
Looking for a Lightweight Microsoft Office Alternative?
While Microsoft Access is robust for database design, troubleshooting VBA errors can be complicated. If you are looking for a reliable, free, and highly compatible office suite for managing data and documents seamlessly, WPS Office is highly recommended.
- 1. Download WPS Office: Visit the official WPS website and download the free WPS Office installer.
- 2. Manage Data with WPS Spreadsheet: Open WPS Spreadsheet to organize, analyze, and manage structured data efficiently as an alternative to complex databases.
- 3. Automate Tasks Using VBA: Press Alt + F11 in WPS Spreadsheet to access the built-in VBA editor and create macros without the strict limitations of Access.

Frequently Asked Questions
Why does Microsoft Access display an 'Undefined function in expression' error?
This error generally occurs when the function name is spelled incorrectly in your query/macro, the function is declared as Private instead of Public, or the VBA module housing the function shares the exact same name as the function itself.
Can I use a Private VBA function in an Access query?
No, a Private function can only be called by other procedures within the exact same module. To use a custom VBA function in an Access query, form, or macro, it must be declared as a Public function.
How do I fix the 'Compile error: Can't find project or library' message?
This error signifies a missing VBA reference. Open the VBA Editor, navigate to Tools > References, and look for any checked items labeled 'MISSING'. You must uncheck the missing reference or browse to link the correct file path to resolve it.




