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 bein
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.
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.
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.
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
Another option: do a rename of the autoexec macro in your VB code.
OpenCurrentDatabase ("Your database")
DoCmd.Rename "Autoexec", acMacro, "tmp_Autoexec"
CloseCurrentDatabase
After you've done your action do a rename back to autoexec again ... et voila....