Run Access Macro from VB Script

abesimpson

Active Member
Joined
May 3, 2003
Messages
435
For a variety of reasons I would like to start Access and various macros from a Visual Basic Script (VBS), or similar.

I have found the following code on the internet, but obviously I can't get it to work

Code:
set objaccess = createobject("Access.application")
objaccess.opencurrentdatabase ("E:\AO_VERIFICATION\AO.mdb")
objaccess.run "AO" ' >> Macro name
objaccess.CloseCurrentDatabase
set objaccess=nothing

Error:...can't find procedure AO... The macro name is AO, and the access path is correct.

Any suggestions as to how to fix it?

Thanks as always

a
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I use this, which appears almost identical to what you have except I do not run a macro. I call a sub procedure.

Code:
dim accessApp
set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDataBase("F:\Your File Path\Your DB Name.mdb")

accessApp.Run "SubName"
accessApp.Quit
set accessApp = nothing

I would try to rename the macro. Maybe because your DB and macro are named the same thing access is getting confused? (Obviously that is just a guess.)

The other thing you may try is look into the syntax of this statment:

Code:
objaccess.run "AO"

Based on this error: Error:...can't find procedure AO

I am thinking that maybe to call a macro, you may need:

Code:
objaccess.run "macro.AO"  

'or 

objaccess.runmacro "AO"

Just some ideas.

HTH
Roger
 
Upvote 0
I converted the macro to a vbs function which I saved in module 2 as "run()". Then I used your code.

Trying to run the code I get an error at Line: 5 Char: 1, ... Access can't find the proceedure 'run'.

Any suggestions?

Thanks in advance for your help.


a
 
Upvote 0
Could you please post the exact code?

Thanks,
Roger

Edit: You could also try: Docmd.run "module2.run"
 
Last edited:
Upvote 0
Code:
dim accessApp
set accessApp = CreateObject("Access.Application")

' accessApp.visible = True

accessApp.OpenCurrentDataBase("Z:\My Virtual Machines data\VM find stock signals\office\portfolio lists\Current Portfolios.mdb")
accessApp.Run "run"
accessApp.Quit
set accessApp = nothing

You will notice the extra line of code "accessApp.visible = True" I added (now commented out). This line confirms that the database "Current Portfolios.mdb" is in fact being addressed.

Thanks for your help.

abe
 
Upvote 0
That was that (function to sub); the devil as they say is in the details.

Many thanks for the touch of magic.

a
 
Upvote 0
There is, but under tools | options click the general tab and check the compact on close. That will do the same thing when the DB closes and you don't need VBS. That might suit your needs.

Thanks,
Roger
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,935
Members
449,134
Latest member
NickWBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top