Run macro from another, already OPEN database (1 Viewer)

urobee

Registered User.
Local time
Today, 12:41
Joined
Aug 12, 2019
Messages
20
Hy,

I have a database (Database2) which is referenced to an other database (Database1) so I can run macros from Database2 in Database1 just like this:
Code:
Database1.TestMacro
(I use it to open a form and run queries.)

So my problem is:
I need to run a macro in Database2 but the button which is activate the macro is on a Form in Database1. Unfortunatelly it seem referenced database is a one way method it don't works when I try:
Code:
Database2.SecondMacro

All of the Google findigs are written to open the database then run the macro, but my database is open already so when i run it got errors.

For example:
Code:
Dim app As Access.Application
    
    Set app = New Access.Application
    
    With app
       '.OpenCurrentDatabase "C:\AccesDB\Database2", True
        .DoCmd.RunMacro "SecondMacro"
        '.CloseCurrentDatabase
    End With

When I tried to skip .OpenCurrentDatabase part of code i got errors.

Any ideas?
Thank You!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:41
Joined
May 21, 2018
Messages
8,527
you can use getObject or just add the external db as a reference.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:41
Joined
May 7, 2009
Messages
19,230
Code:
Option Explicit

Dim app As Access.Application

Private Sub button_Click()
    If app Is Nothing Then _
        Set app = New Access.Application
        app.OpenCurrentDatabase "C:\AccessDB\Database2.accdb", False
        app.DoCmd.RunMacro "SecondMacro"
        app.Visible = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
If Not (app Is Nothing)
    app.CloseCurrentDatabase
    app.Quit
    Set app = Nothing
End If
End Sub
 

urobee

Registered User.
Local time
Today, 12:41
Joined
Aug 12, 2019
Messages
20
@arnelgp
Thanks but it works like my other code. It's open again the Database2 then i got an error: 2485- Cannot find the object 'SecondMacro.'

Shouldn't I need to refer somewhere in the code to the form where is the actual "SecondMacro" ?

I'm confused :confused:
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 03:41
Joined
Oct 29, 2018
Messages
21,467
@arnelgp
Thanks but it works like my other code. It's open again the Database2 then i got an error: 2485- Cannot find the object 'SecondMacro.'

Shouldn't I need to refer somewhere in the code to the form where is the actual "SecondMacro" ?

I'm confused :confused:
Hi. Are you able to post a sample copy of both databases for us to examine? We just need the parts needed to duplicate the problem and maybe fix it.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:41
Joined
May 7, 2009
Messages
19,230
check the macro name in Database2.
 

urobee

Registered User.
Local time
Today, 12:41
Joined
Aug 12, 2019
Messages
20
Unfortunatelly I don't have permission to share parts of the code.
The macro name is good. I've tried with a simple msgbox "hello" macro too from a different modul, doesn't work either, I got the error 2485- Cannot find the object 'HelloTest.'
 

urobee

Registered User.
Local time
Today, 12:41
Joined
Aug 12, 2019
Messages
20
Update:

I think half of the problem is solved.
At this time there is no another instance of Access when I click the button.
But this error still not solved:

2485- Cannot find the object 'Hello.'
(It would be just a test with a simple msgbox message to check the macro is works fine.)

There is two things i discovered
-The error message says cannot find the OBJECT.
-There is a "." after the macro name in the message.

I've tried to rename the macro, changed the Sub from Private to Public, changed form Sub to Function without any success.

Here is my actual code:

Code:
Dim app As Access.Application
Set app = GetObject("C:\AccessDB\Database2.accdb").Application
app.DoCmd.RunMacro ("Hello")
It's doesn't load another instance of Access just change to the already opened database. So this part of the problem is solved :)

This is the very complicated macro I wanted to run (It's just a test of course)
Code:
Private Sub Hello()
MsgBox "HELLO"
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:41
Joined
May 7, 2009
Messages
19,230
make the sub Public.

app.Run "hello"
 

urobee

Registered User.
Local time
Today, 12:41
Joined
Aug 12, 2019
Messages
20
Thanks for the help, You are the best Guys!


The final code:

Code:
Dim app As Access.Application
Set app = GetObject("C:\AccessDB\Database2.accdb").Application
app.Run "Hello"
 

Users who are viewing this thread

Top Bottom