Access VBA Macro Programming

(Joao Candeias) #1

If you imagine that in Access the forms and reports take the place of dialogs and
worksheets in Excel, then you can also have modules attached to these objects. Once the
module is created on a form or a report, it will show up in the tree of objects on the left-hand
side of the VBE window.
One of the problems in Access is that code can appear in a number of different places.
Modules can be inserted via the VBE window, or they can be added to forms or reports.
Provided the form or report is open, all the code modules are interactive and program flows
can often cross from one module to another or, for example, from one form to another.
For example, a subroutine may call another subroutine or function that is in another
module. That action will be performed and then the program flow will move back to the
original procedure it was called from.
This can make it quite hard to keep track of exactly what is going on in an Access
application and debugging code can become a very complex task. It is certainly more difficult
to pick up an existing Access application and find out how it all works.
Remember, VBA is an object-oriented language. This means that the language works in
terms of various objects. For example, the Access application is an object, a query definition
is an object, as is a table definition. When using VBA, you use these objects and their various
properties, methods, and events to create your code.
The first branch on the tree coming from the root of the VBA project says Microsoft
Office Access Class Objects. Coming off this branch are objects for the modules that have
been created in the VBE. However, as you add in forms and reports and add modules to these
objects, you will see them reflected in the tree structure.
This is a very important concept to understand because all the forms and reports you
create are objects that can be referred to. These are not the only objects within Access, but
looking at the Project Explorer simplistically, these are the objects shown there.
On a brand new Access database that you have just created, no modules will be visible.
Click Insert | Module on the menu bar to insert a module.
Initially, the module does not show a great deal, and you may wonder what to do with it. If
you type something at random such asWhat do I do now?and pressENTER, you will get a
compile error. This is because there are disciplines and rules about entering code. Everything
you enter here goes through a Visual Basic compiler that interprets what you have written
and converts it into instructions that your computer understands. Unfortunately, it does not
understand plain English, which is why you get a compile error.
Click OK in the Compile Error message box and delete your statement. Notice the
statement line turned red when the compile error appeared to draw your attention to the
problem. Even if you do nothing about it, it will remain red as a danger signal to show there
is a problem with your code.
Your screen should now look like Figure 1-4.
The first step in entering some VBA code is to enter a subroutine called MyCode. You do
this by simply typing inSub MyCodeanywhere in the module screen and pressingENTER.
VBA will automatically add End Sub and the brackets after MyCode.


Sub MyCode()
End Sub


Chapter 1: The Basics 7

Free download pdf