Home » excel » Calling Access Sub from Excel

Calling Access Sub from Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to call a subroutine in an Access database from Excel. This sub then call a number of other subroutines, all contained within the database. I saw a number of other posts where this was discouraged, but for reasons, Excel needs to be the front-end for this. I tried:

Sub TestRun()
        Dim acObj As Access.Application
        Set acObj = CreateObject("Access.Application")
        acObj.Application.Visible = True
        acObj.OpenCurrentDatabase "C:\testMDB\TEST.mdb", False, "password"
        acObj.Application.Run ("TestRunAccess")
End Sub

The database is part of a workgroup with a password – running it this way still prompts for the password. I’m not very familiar with Access – how would I go about doing this? What references would I need to include?

How to&Answers:

That would be:

''Reference: Microsoft Access x.x Object Library
Dim acObj As New Access.Application
''Set acObj = CreateObject("Access.Application")
acObj.Application.Visible = True
acObj.OpenCurrentDatabase "C:\testMDB\TEST.mdb",,"ADatabasePassword"
acObj.Application.Run "TestRunAccess"

You may prefer to use late binding if you wish to avoid problems with references, in which case:

Dim acObj As Object
Set acObj = CreateObject("Access.Application")
acObj.Application.Visible = True
acObj.OpenCurrentDatabase "C:\testMDB\TEST.mdb",,"ADatabasePassword"
acObj.Application.Run "TestRunAccess"