Open a macro in a databese2 in database1 (1 Viewer)

mansied

Member
Local time
Today, 01:37
Joined
Oct 15, 2020
Messages
98
Hello
I have two databases,1and 2,I want to run a macro from database 2 in database 1 :
I have the below code, but not work and no error .can you hint where is the issue?

Public Function run_main()
Call CallMacro()

End Function

Public Sub CallMacro()
'
Dim strDbName, strMacro
Dim objApp
'
strDbName = "*****.accdb"
strMacro = "Main"
'
Set objApp = CreateObject("Access.Application")
'
objApp.OpenCurrentDatabase strDbName
'
' Run Macro:
'
objApp.DoCmd.RunMacro strMacro
'
objApp.Quit
Set objApp = Nothing
'
End Sub
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:37
Joined
Mar 14, 2017
Messages
6,454
Maybe it did work. What does it do?
 

mansied

Member
Local time
Today, 01:37
Joined
Oct 15, 2020
Messages
98
from database 1 It should open database2 and run macro "main" on it.
it doesn't open database2.
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:37
Joined
Mar 14, 2017
Messages
6,454
Excellent @isladogs good catch!
I think OpenCurrentDatabase would be more appropriate if it were being used as a method to Set a database variable?
 

mansied

Member
Local time
Today, 01:37
Joined
Oct 15, 2020
Messages
98
I changed it and the result:

1620847927925.png


1620847947614.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:37
Joined
Sep 21, 2011
Messages
10,531
Wouldn't you need the full path of the dB file anyway?
 

isladogs

CID VIP
Local time
Today, 06:37
Joined
Jan 14, 2017
Messages
16,216
Wouldn't you need the full path of the dB file anyway?
Good point. Yes you would need the full path
You might also be able to open direct to the required macro using the /macro switch
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:37
Joined
Mar 14, 2017
Messages
6,454
I use OpenCurrentDatabase with no problem

Solved - Open External Access Database | Access World Forums (access-programmers.co.uk)

Here is an actual VBScript file that I use currently (scheduled via Windows Task Scheduler) - I've only made changes to sanitize/redact, but it's actual code in-use.

Code:
dim accessapp, db

set accessapp=createobject("access.application")
accessapp.visible=true
accessapp.opencurrentdatabase("\\server\folder\folder\DBName.accdb")
accessapp.Run "NameOfSUB"
accessapp.closecurrentdatabase
accessapp.quit
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:37
Joined
Sep 21, 2011
Messages
10,531
So O/P is not using () and access thinks xxx.accdb is a property or method?
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:37
Joined
Mar 14, 2017
Messages
6,454
So O/P is not using () and access thinks xxx.accdb is a property or method?
Yeah, good point ... Maybe that's the problem.

I don't know what I was thinking earlier, my own code proved my comment wrong. My memory is very unreliable. :)
 

isladogs

CID VIP
Local time
Today, 06:37
Joined
Jan 14, 2017
Messages
16,216
I use OpenCurrentDatabase with no problem

Solved - Open External Access Database | Access World Forums (access-programmers.co.uk)

Here is an actual VBScript file that I use currently (scheduled via Windows Task Scheduler) - I've only made changes to sanitize/redact, but it's actual code in-use.

Code:
dim accessapp, db

set accessapp=createobject("access.application")
accessapp.visible=true
accessapp.opencurrentdatabase("\\server\folder\folder\DBName.accdb")
accessapp.Run "NameOfSUB"
accessapp.closecurrentdatabase
accessapp.quit

So do I. My only comments on your code are not defining your variables explicitly (so they are variants) and it may help that you made Access visible before using OpenCurrentDatabase (though I don't in the code below as its already open)

FWIW here is some generic code I use to open an external database from Access in different ways...hence much has been commented out

Code:
Public Function RunExternalDatabase() As Boolean
  
    Dim app As Access.Application, strPath As String
  
    'Start a new MSAccess application
    Set app = New Access.Application
  
    'Open the remote database, then close the current (or remote) database
    With app
        'Syntax: .OpenCurrentDatabase(filepath, Exclusive, strPassword) - the last 2 items are optional
      
        strPath = "C:\Programs\MendipDataSystems\JATFA\JATFA.accdb" 'replace with your file path
       ' strPath = "C:\tmp\DB2.accdb" 'full file path to your database
         .OpenCurrentDatabase strPath, True 'no db password
     '   .OpenCurrentDatabase strPath, True, "password" 'for use if password exists
  
    '    .DoCmd.RunMacro "mcrRefreshPersTable" 'run your macro
       ' .CloseCurrentDatabase 'closes external database as that is current
    End With
  
    'Quit the spawned app - DISABLED as not wanted here
    'app.Quit acQuitSaveNone
    'Set app = Nothing
  
    'Quit the current app
    Application.Quit acQuitSaveNone
  
End Function
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:37
Joined
Feb 19, 2002
Messages
36,284
What does the macro do? If it doesn't open a form and you don't need any response, you can create a batch file to open the database with the /m switch to run the specified macro and run the batch file.
 

Users who are viewing this thread

Top Bottom