Datasheet

Chapter 1
42
Using VBA makes your database easier to maintain.
Macros are completely separate from the objects that call them. Although we created the
navigation button macro from within the form, the macro is actually stored as a separate object
in the database window. Click the Macros tab and you'll see it's there. In contrast, you can save
VBA code with the form itself. This means that if you want to move the form into another
database, the code automatically goes with it. With macros, you would have to find out for
yourself which macros you needed to take as well.
Using VBA allows you to interact with other applications.
With VBA you are able to make full use of Automation. This facility allows you to access the
functionality of applications like Excel and Word from within your Access application. It also
allows you to control Access programmatically from applications like Excel and Word. More on
this in Chapter 15.
Using VBA gives you more programmatic control.
Macros are good at performing set tasks where there's little need for flexibility. They can't pass
variables from one macro to another in the form of parameters, are unable to ask for and
receive input from the user, and they have extremely limited methods for controlling the
sequence in which actions are performed.
VBA is easier to read.
Because you can only view one set of Action arguments at a time in the lower pane of the macro
window, it is difficult to see the details of a macro. You have to select each action one after the
other and look at its arguments in turn. In contrast, VBA is very easy to read with its color-
coded text and Full Module View.
VBA is common to all Microsoft applications (well, almost!)
Finally, VBA is the language on which all Microsoft applications are now standardizing. VBA
code written in Access is easily portable to Excel, Word, and any other applications that use
VBA (we shall be showing you more about this in Chapter 15). In contrast, macros are highly
specific to their native application.
When to Use Macros
By this stage, you may be wondering why you should ever bother to use macros if VBA has so
much in its favor! Well, there are still a couple of things that you can't do in VBA that you need
macros for, and we'll look at these below. They are:
Trapping certain keystrokes throughout the application
Carrying out a series of actions whenever a database is opened (this is done via the
Autoexec macro)