Code to run Access macro from Excel

I need to run a set of Access macros from an Excel sheet. Trying to get just one to work for a start. I found some code which looks like it should work. The code should open Access, open the specified db, run a specified macro, close the db. It opens Access ok but then puts up a warning "can't find mac_xxx" without apparently even opening the db. The db and macro names match what's in the code. Any ideas? I'm using Access 2013. I think the code may have been written for a previous version. Code below.

Sub Run_Access_Macro()

         'Opens Microsoft Access and the file Fred.accdb
            Shell ("C:\Program Files (x86)\Microsoft Office\Office15\msaccess.exe C:\Documents\Fred.accdb")
         'Initiates a DDE channel to Microsoft Access
            Chan = DDEInitiate("MSACCESS", "system")
         'Activates Microsoft Access
             Application.ActivateMicrosoftApp xlMicrosoftAccess
         'Runs the macro "mac_XXX" from the Fred.accdb file
             Application.DDEExecute Chan, "mac_XXX"
         'Terminates the DDE channel
          Application.DDETerminate Chan

      End Sub
AngeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Dim A As Object
Set a = CreateObject("Access.Application")
'//Use next statment only needed if you want to see Access - default is to be "invisible"
a.Visible = False
a.OpenCurrentDatabase ("C:\Documents\Fred.accdb")
a.DoCmd.RunMacro "mac_XXX"
a.CloseCurrentDatabase
a.Quit
Set a = Nothing

Open in new window

Regards
AngeAuthor Commented:
Hi,
I tried this code. I get "Run time error 2501 - the Run Macro action was cancelled."

Debug highlights A.Do.Cmd.RunMacro "mac_xxx"

Any ideas?
Rgonzo1971Commented:
Hi,

why not transform the macro in VBA

and use

a.Application.Run "mac_XXX"
instead of
a.DoCmd.RunMacro "mac_XXX"
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

AngeAuthor Commented:
Hi
I don't understand or know how to "transform the macro in VBA".

I changed the code as above but hit another error, 2517 "Access can't find the procedure "mac_xxx".

?
Rgonzo1971Commented:
Could you send a screenshot of your macro
AngeAuthor Commented:
I've found a neat solution using a .bat file which looks like this:

set MSACESS="C:\Program Files\Microsoft Office 15\root\office15\MSACCESS.EXE"
%MSACCESS% "C:\DPM\Fred.accdb" /x mac_Fred
%MSACCESS% "C:\DPM\Bert.accdb" /x mac_Bert

I've got all my dbs and macros lined up and it runs through them in seconds.

thanks for the ideas on other solutions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.