|
vb123.com
Garry Robinson's Popular MS Access, Office and VB
Resource Site
 |
|
Home
Contact Us
Order our Software
RSS &
Newsletter
Join our XML/RSS Newsfeed or sign up for our informative newsletter on
Office Automation, Access and VB topics
Read More
Get Good Help
If you need help with a database, our Professionals could be the answer
Read More
Is Your Database Corrupt ?
If you have a corrupt database,
Try our Access Recovery
service
The
Workbench
Find out who has your database open, start the
correct version of Access, easy compacting and backups, change startup
options, mde compile, shutdown database
Read and
Download
The Toolshed
Searchable help file comprising of all the information at vb123.com plus
hidden downloads etc. Read More
The Toolbox
Libraries of software that we regularly import into our projects.
Enhances the Toolshed More..
DryToast New
Backup and query your BaseCamp®
projects
Read More
Datamining/Graphs
Explore your data with this versatile graphing and data mining shareware
tool. Read More
Garry's Blog
Find out a few other things that
Garry has been writing about Microsoft Access.
Read more
About The Editor
Garry Robinson
writes for a number of popular computer magazines, is now a book author
and has worked on 100+ Access databases. He is based in Sydney,
Australia
Contact Us ...
Search ...
or try our new Expression Web
vb123.com.au
|
| |
The AutoExec Macro Provides a Flexible Springboard
This is a sample from the
Microsoft Access Protection book by Garry
Robinson
For those of you who started your
programming life by using DOS as an operating system, you will probably be quite
familiar with the autoexec.bat file, a file used to configure your PC. Though
this batch file is almost a history lesson now, Access has a similar starting
mechanism called the AutoExec macro. This macro has been around since Access 2
and is still an important foundation for protecting your database. When your
database opens, Access first processes the startup options and then looks for
and runs the AutoExec macro.
I consider the AutoExec macro to be an important
protection mechanism because it acts as the springboard for your application.
For example, you can simultaneously launch your startup form and any other
procedures that you need to maintain your security and software integrity (see
the working example that follows).
Of course, like most other things that you do in
Access, there is an alternative way to add startup procedures to your
application. Generally, this other method involves using VBA code that is
processed in your startup form's onOpen event. If you want to see an example of
this technique, see the onOpen event in the Northwind database's startup form.
Creating Your First AutoExec Macro
If your database doesn't have an AutoExec
macro, create a sample to see how it works:
-
Open the Database window and select
Macros.
-
Create a new macro by clicking New.
-
Select a check box in the Action column
and type (or choose) "MsgBox."
-
In the bottom pane, you will find the
custom action arguments suited to the action that you selected.
-
In the message box in the bottom pane,
type "AutoExec is running."
-
Now close the macro and save it as
AutoExec.
-
Close your database and then open it
again.
-
You should now see the message "AutoExec
is running."
Creating a Good AutoExec Macro
A good AutoExec macro can launch one or
many different procedures or objects. In my view, the actions in the macro
should either open a form or run a VBA function. Though a macro can do many
things, I recommend that you restrict this macro to simple tasks and leave
complex tasks to VBA.
If you open the demonstration database, you
will find an AutoExec macro that opens two forms and runs a function called
HideDBObjects.
Figure 4-1 shows the AutoExec
macro with the Expression Builder. It is important to note that Access macros
will allow you to call only a function, rather than a subroutine or class
module.
| |
Note |
If you don't want to run the AutoExec
macro when you open the database, press SHIFT when the database opens. You
can set the Allow ByPass option to False to avoid this security breach
(described in Chapter 2). |
How I've Used AutoExec Macros
When I started researching the AutoExec
macro for this book, I thought I'd better review the databases that we have
developed over the years. Not surprisingly, most of those databases used an
AutoExec macro to control the startup actions. During the eight years that
I've been programming Access databases professionally, I can't recall ever
having had any problems with the AutoExec macro itself. As for the procedures
and forms that the AutoExec macro runs, now that's
been a different story. So what have I used the AutoExec macro for?
 |
Opening the database's main form. |
 |
Minimizing the Database window. |
 |
Modifying the startup options and other
protection settings (just in case of user tweaking). |
 |
Checking for VBA project reference
failures. |
 |
Opening the user logging form (see
Chapter 6). |
 |
Checking for regional date settings. |
| |
Note |
An Autoexec macro runs only when
you open the database or if you specifically run it from the Database
window, from code, or from another macro. It will not run at any other
time. |
Using the AutoExec Macro to Launch Your
Startup Form
In my view, the startup options are more
accessible to the user than the AutoExec macro. Because of that, I recommend
that you launch your startup form from the AutoExec macro. If you launch a
form from both the Startup dialog and the AutoExec macro, the startup form
will load first. Although it's possible to launch other code from your startup
form, I don't recommend it because the form can be rewritten or lost.
| |
Tip |
The macro environment is a good
place to learn about the wide variety of functions that you can write in
VBA code. I recommend that you work through the different macro actions
(try the drop-down list in the Action column), and if an action attracts
your interest, press F1 for help. |
These samples are discussed at
length in Chapter 4 of Garry's Book on Access
Protection and Security
Read More
You should also try out the simple backup process that comes with
the
Access Workbench
Other Pages At VB123.com That May Be Of Interest
Access
2000/2002/2003 Export To Text Bug/Safety Measure
Take the SQL Taste Test in
Access ADO
The UserForm - How to setup a
difficult to decode password entry form Click on the following button
to jump to the next page in the protection samples loop.
|