Here is another common question:
How can I run/execute a Macro in another external database?
Well, like with most MS Access related things, there are several possible solutions.
Copy them into the Current Database
In some cases, the solution is as simple as copying the macro into the current database and creating linked tables to the data required by the Macro. This way you can actually run the macro locally. No more need to execute anything remotely.
Use the /x Command Line Switch
Another solution is to use the Shell command in conjunction with the /x command line switch to run a specified macro. The problem with this approach is that it will leave the external database running after completing the execution of the macro. Whether this is desirable or not depends on each situation. Below is a sample function to shell out a Macro:
'---------------------------------------------------------------------------------------
' Procedure : ShellExecExternalMacro
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Run a Macro in an external database
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sDb : Fully qualified path and file name with extension of the external database
' that you want to run a macro from
' sMacroName: Name of the Macro to run
'
' Usage:
' ~~~~~~
' Call ShellExecExternalMacro(Application.Currentproject.Path & "\Database2.accdb", "Macro1")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-Sep-16 Initial Release
'---------------------------------------------------------------------------------------
Function ShellExecExternalMacro(sDb As String, sMacroName As String)
On Error GoTo Error_Handler
Call Shell("msaccess.exe " & sDb & " /x " & sMacroName, 1)
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: ShellExecExternalMacro" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Useful References:

Use a Custom VBA Function
The best solution in my opinion is a simple custom function in which you can specify whether or not to close the external database instance. Below is a sample function to do so:
'---------------------------------------------------------------------------------------
' Procedure : ExecExternalMacro
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Run a Macro in an external database
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding -> None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sDb : Fully qualified path and file name with extension of the external database
' that you want to run a macro from
' sMacroName : Name of the Macro to run
' bCloseExtDb : Whether or not Access should close the instance of the external database
'
' Usage:
' ~~~~~~
' Call ExecExternalMacro(Application.Currentproject.Path & "\Database2.accdb", "Macro1", True)
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-09-16 Initial Release
' 2 2022-08-08 Bug in variable naming in Error_Handler_Exit
' Updated Copyright, Refs, Error_Handler
' 3 2025-01-23 Corrected the Usage example - Thank you Rhett
' for pointing that out.
'---------------------------------------------------------------------------------------
Function ExecExternalMacro(sDb As String, sMacroName As String, Optional bCloseExtDb As Boolean = True)
On Error GoTo Error_Handler
Dim oAccess As Object 'New Access.Application
Set oAccess = CreateObject("Access.Application")
oAccess.OpenCurrentDatabase sDb
oAccess.DoCmd.RunMacro sMacroName
Error_Handler_Exit:
On Error Resume Next
If bCloseExtDb = True Then
oAccess.CloseCurrentDatabase
oAccess.Quit
End If
Set oAccess = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: ExecExternalMacro" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
So as you can see, there are a couple possible approaches to running a Macro in an external database, but VBA is involved (sorry Web Apps this is yet one more thing you can’t do!).
Great Post! Everything worked great but I had to do one thing: Change the name of the call statement: Call OpenExternalMacro(Application.Currentproject.Path & “\Database2.accdb”, “Macro1”, True)
This doesn’t match the actual function: Function ExecExternalMacro(sDb As String, sMacroName As String, bCloseExtDb As Boolean)
your call should be ExecExternalMacro and not OpenExternalMacro just so its correct.. thank you very much
Your routine works GREAT!…
But I was originally getting an error because the external macro I was calling used the QUIT command which was killing the automation call with “The RunMacro command was canceled” error.
Once I created then called a new version of that eternal macro with out the QUIT command and used the 3rd parm to accomplish the close for the automated access object… then it worked perfectly.
You could open the backend database from the frontend and run the macro, using an ‘Application’ object.
That’s exactly what the ‘Use a Custom VBA Function’ does. How you determine the sDb string is up to you, but the function open the specified database and runs the macro.
Resolved many problems with this code.
What I do not understand is the bCloseExtDb – how is it set in order to check if True or False?
And where is appAccess defined so that appAccess is set to nothing.
It worked any ways, but I could use a narrative for these.
Rick
Like it is defined in the function header:
So when you call the function, you specify True or False for bCloseExtDb where True means close the instance of the external database when done (after running the macro) or False which means leave it alive. 99.9% of the time it should be set to True.
appAccess is a mistake in my code and I’ve updated the code to resolve the issue. Thank you for pointing it out.
Hello Daniel,
Thanks for your insights, very helpful as always!
Nevertheless I have some problems:
– We continue to use the old .mdb/2003-format databases with the built-in security (users/groups/passwords). I couldn’t find a way to pass a username/password using the VBA function to open another base.
– The command line approach doesn’t work because an existing autoexec macro is still run first.
Also with the function, the new Access instance remains open; adding “oAccess.Quit” closes it.
Thank you, Markus