MS Access: VBA Basics in Access 2003/XP/2000/97

What is VBA?

VBA standards for Visual Basic for Applications and is the language embedded within your Access database.

You use VBA whenever you do one of the following:

  1. Create a new function.
  2. Create a new subroutine.
  3. Define a global variable.
  4. Place code behind an event procedure such as the "On Click" event of a command button.
  5. Execute the RunCode action in a macro.

These are just some of the examples of when you might be running VBA code.


How to get to the VBA environment

You can access the VBA environment by opening up the Microsoft Visual Basic window. The quickest way to do this is by pressing Alt-F11 while your Access database file is open.



This is an example of what the Microsoft Visual Basic window looks like.


Next, let's take a few moments to analyze the various sections in the Microsoft Visual Basic window.

Project Explorer


The Project Explorer can usually be found in the top left portion of the Microsoft Visual Basic window. It is a hierarchical listing of the objects recognized by VBA.

In this example, there are two "Microsoft Access Class Objects" - one is a form called frmProducts and the other is a report called rptCategories_Report. There is also one Module called Module1.

These are all objects that you've created in your Access database.

If the Project Explorer is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Project Explorer under the View menu.



Properties Window


The Properties window is usually found directly below the Project Explorer. It displays the properties for the object currently highlighted in the Project Explorer. In the example above, it is displaying the properties for the module called Module1.

If the Project Explorer is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Properties Window under the View menu.




Code Window


The Code window is usually found to the right of the Project Explorer. It displays the VBA code for the object currently highlighted in the Project Explorer. In the example above, it is displaying the VBA code for the module called Module1.

If the Code window is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Code under the View menu.



Immediate Window


The Immediate window is usually found below the Code window. It is an essential element of the debugger found within the VBA environment. It lets you:

  • Type code and press ENTER to view the results of the code.
  • When in debug mode, it lets you view the value of a variable in its current state. This will be discussed in the tutorial on Debugging VBA Code.

If the Immediate window is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Immediate Window under the View menu.




Objects displayed in the Project Explorer

The next important aspect to discuss is what objects appear in the Project Explorer.

At first glance, you'd assume that all of the Forms, Reports, and Modules that you create will automatically appear in the Project Explorer. But this is not true. The VBA environment only knows about the objects that you tag as "having a module".

So by default, all Modules will appear in the Project Explorer, but not all Forms and Reports necessarily will.

A Form will only appear in the Project Explorer if the Form's "Has Module" property is set to "Yes".



Similarly, a Report will only appear in the Project Explorer if the Report's "Has Module" property is set to "Yes".



Now, you're probably asking yourself, "How come some of the forms and reports in my Access database have the "Has Module" property set to "Yes" and others don't?"

Well, anytime that you invoke the Code Builder in a Form/Report, Access automatically sets Form/Report's "Has Module" property to "Yes".


Therefore, if you want to reference one of your Forms or Reports in the VBA environment, you may need to manually go to the Form/Report's Properties window and set the "Has Module" property to "Yes".

Congratulations! You've just completed the tutorial on understanding the VBA environment.