views:

280

answers:

4

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.

+2  A: 

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.

ChuckB
very sneaky :) +1
Praesagus
+1  A: 

Code run at start-up may not be in an autoexec macro, it may be in a startup form, therefore, you may wish to consider 'programmatically pressing the shift-key' through APIs:

http://www.mvps.org/access/api/api0068.htm

Remou
A: 

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.

Harry Steinhilber
A: 

This is an old question, but another way to do this would be to use the /cmd commandline argument, and use a COMMAND statement in the app's startup to conditionally run startup routines depending on whether there was a /cmd argument provided. This would only work with a commandline startup, though, not via automation.

David-W-Fenton