Datasheet

Designing Applications
45
If you want to perform an action whenever the database is opened, but want to get the benefits
of using VBA rather than macros, then you should write a procedure in VBA and call the
procedure from the Autoexec macro. You can do this by using the RunCode action in your
Autoexec macro:
In this situation, when the database opens, the Autoexec macro is executed and this causes the
MyCode() procedure written in VBA to be executed.
Be aware, however, that a user can prevent the Tools/Startup... options or
the Autoexec macro from running by holding down the Shift key when the
database is being opened.
You can prevent a user from bypassing the Tools/Startup... options and the Autoexec macro by
setting the database's AllowBypassKey property to False. However, the AllowBypassKey
property isn't available normally, and so can't be set in the usual way. We'll look at how to set
this property from VBA later on.
Moving to VBA
Macros have their purposes then, but while undoubtedly useful for some things, they don't
offer the power of VBA. We've just demonstrated in a few pages how and where you should
apply macros. We'll now use the rest of the book describing how and where you can use VBA.
Just before we do that, however, there is one last useful trick that macros can give us: Access
will allow us to convert macros into VBA code, and it can even go one step further by adding
useful error trapping code if we ask it to. This can be a very useful technique for those new to VBA.