Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip Autoexec macro when opening MSAccess from MSAccess?

So I have an MSAccess MDB that needs to open other MDB's and run a bunch of code that will compare two Access MDB's to find code differences,query diffs,etc. The goal being to verify any production MDB has not been altered from the original deployment.

My problem is that many of these Access apps have Autoexec macros and there is no simple way to call .OpenCurrentDatabase without running the autoexec macro.

How can I just skip the macro using CODE?

I know I can hold down the shift key. I know I can turn that option on and off too.

like image 879
ChuckB Avatar asked Mar 11 '10 02:03

ChuckB


People also ask

What is AutoExec macro in Access?

An AutoExec macro is just a macro that is named AutoExec. When a database starts, Access runs the AutoExec macro before it runs any other macros or VBA code.


2 Answers

It's a sneaky solution but it works for me.

I do a DoCmd.DatabaseTransfer acImport of the Autoexec macro. Then I replace the autoexec with a blank one, using DoCmd.DatabaseTransfer acExport

The trick is that an Export will overwrite

DoCmd.TransferDatabase acImport, "Microsoft Access", sSourcePath, acMacro, "autoexec", "autoexecSource"

DoCmd.TransferDatabase acExport, "Microsoft Access", sSourcePath, acMacro, "autoexecblank", "autoexec"

I do that again for the second MDB

DoCmd.TransferDatabase acImport, "Microsoft Access", sDestPath, acMacro, "autoexec", "autoexecDest"

DoCmd.TransferDatabase acExport, "Microsoft Access", sDestPath, acMacro, "autoexecblank", "autoexec"

Then I can do all the comparisons for finding Diffs between the two MDBs without triggering the autoexec macros since I imported them and replaced them

Then I compare the two Macros I imported and then export them back to the databases.

DoCmd.TransferDatabase acExport, "Microsoft Access", sSourcePath, acMacro, "autoexecSource", "autoexec"

DoCmd.TransferDatabase acExport, "Microsoft Access", sDestPath, acMacro, "autoexecDest", "autoexec"

Obviously this solution only works using VBA in Access, but it does work.

Hope this helps someone.

like image 77
ChuckB Avatar answered Oct 27 '22 08:10

ChuckB


Another option if you have access to edit the AutoExec macros of the databases you are opening: set a condition on each step in the macro that specifies [Application].[UserControl]. By specifying this, the macro step will only run if the database is opened by a user, not through automation. If the macro step already has a condition set on it, you can just put parenthesis around it: (old condition) AND [Application].[UserControl].

If you don't have the ability to change those macros, though, you may be better off following @ChuckB's solution.

like image 26
Harry Steinhilber Avatar answered Oct 27 '22 09:10

Harry Steinhilber