O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.2. Variables and VBA Syntax

This section goes over some basics you need to know to program successfully in VBA. Most of them need only to be recapitulated, so if you need more information about any of these topics, you might want to purchase a beginner's guide to VBA programming, such as VBA For Dummies, by John Paul Mueller (Wiley Publishing, Inc., ISBN 0764539892). Of course, the help features in Access and VBA also provide guidance on specific tasks or features.

5.2.1. Variables

One of the most important concepts in programming is the use of variables. A variable is a location in memory where you can store a value while your code is running. VBA only needs to find an item's location the first time it is used in code; it does not need to look up the location each time. For example, if you need to specify a particular state repeatedly in your code, it can be much faster and cleaner to create a variable strState than to repeatedly use Washington in your code. Not only will the code run faster, but if you ever need to switch states and use California instead, all you have to do is change the value of your variable rather than find all instances of Washington in your code.

Using variables can also make your code easier to interpret. They not only define what VBA allows in the field, but they let you and other developers know what the field can be used for.

Variables hold a variety of types of data including strings, numbers, and objects. As you'll see shortly, a string variable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required