Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run an Access macro from Excel? 1

Status
Not open for further replies.

AirZebra

Technical User
Apr 23, 2002
31
FR
Hi all,

I have the following code (for which I apologise most sincerely) in an Excel spreadsheet that compacts an Access database to a separate location and then opens it. Is it possible to then run a macro stored in the database from the code in Excel?

Code:
Dim StandardDBM As Database
Dim NewDBM As Database

StandardDBM = "D:\Exported Data\Reports\Standard\ManStandard.mdb"
NewDBM = "D:\Exported Data\Reports\" & LastMonthDate & "\ManReport - " & LastMonthDate & ".mdb"

DBEngine.CompactDatabase StandardDBM, NewDBM
Dim MyDBM As Object
Set MyDBM = GetObject(NewDBM)
MyDBM.Application.Visible = True
Set MyDBM = Nothing[\code]

The macro that I want to run is called mcrFinalise.

Regards,

Alan
 
First make certain that you reference the Microsoft Access Object Library in your reference Library, then try something like this:

Public Sub runACMacro()

Dim acApp As Access.Application

Dim StandardDBM As String
Dim NewDBM As String

Set acApp = New Access.Application

StandardDBM = "D:\Exported Data\Reports\Standard\ManStandard.mdb"
NewDBM = "D:\Exported Data\Reports\" & LastMonthDate & "\ManReport - " & LastMonthDate & ".mdb"

DBEngine.CompactDatabase StandardDBM, NewDBM

acApp.OpenCurrentDatabase (NewDBM)

acApp.DoCmd.RunMacro mcrFinalise
acApp.Quit

set acApp = Nothing
End Sub
 
Thanks BKClaw,

When I add your code and run the macro I get

Code:
Run Time Error 91

Object variable or With block variable not set[\code]

at the
acApp.OpenCurrentDatabase (NewDBM)
line.  Do you have any idea why this could be?

Regards,

Alan
 
Sorry - forget that last reply, it just meant that I didn't have Access open. Now I have Access open it comes up with a run time error 2501 saying something about how I used the runmacro command and then clicked cancel in a dialogue box. Now I know it is Friday afternoon, but I am pretty sure that no boxes appeared on my screen, and I am almost definite that I didn't click on anything that wasn't there.
 
I finally got that last bit sorted out. For any interested parties it seems the database was closing before it got a chance to run the macro. I am awarding BKClaw a star as his reply helped me a lot.

Regards,

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top