Calling Access Sub from Excel

2019-08-07 01:12发布

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?

1条回答
Animai°情兽
2楼-- · 2019-08-07 01:56

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"
查看更多
登录 后发表回答