Greetings,
I have been searching the web and Access relentlessly trying to find a way to call an Access macro from a different Access data base.
Example - The form button gets clicked and the first macro executes. Then a second macro is selected from a closed data base so that the query in the second database can run. Alternatively open the second data base and call the macro.
In other words I need to use several Access databases and want the one with the form to control the others via the onclick command macro.
What you are referring to is Automation Code utilizing Access as an Automation Server.
The following code will run Macro1 in the C:\Test\Test.mdb Database from the current Access Database. As far as I known, Automation is the only way to effectively control Objects in External Access Databases from a Current Access Database. Here are the steps which must be followed in strict sequence.
__1. In a
Form's Declarations Section, declare a variable as Access.Application. This variable will soon be a legitimate reference to an external Access Application.
- Dim objAccess As Access.Application
__2. Place the following code which will create a New Instance of Access and run the Macro in an appropriate Event Procedure.
- Set objAccess = CreateObject("Access.Application")
-
-
objAccess.Visible = True
-
-
'Open Test.mdb exclusively
-
objAccess.OpenCurrentDatabase "C:\Test\Test.mdb", True
-
-
'Execute Macro1 which will open qryEmployees Maximized on screen
-
objAccess.DoCmd.RunMacro "Macro1"
-
objAccess.DoCmd.Maximize
__3. Destroy the previously created Instance of Access when you are finished with it.