417
CHAPTER
Mastering VBA Data
Types and Procedures
IN THIS CHAPTER
Naming and declaring
variables
Looking at the VBA data types
Working with subs and
functions
Building functions
A
ll VBA applications require variables to hold data while the program
executes. Variables are like a white board where important informa-
tion can be temporarily written and read later on by the program.
For example, when a user inputs a value on a form, you’ll most often use a
variable to temporarily hold the value until it can be permanently stored in
the database or printed on a report. Simply put, a variable is the name you’ve
assigned to a particular bit of data in your application. In more technical
terms, a variable is a named area in memory used to store values during pro-
gram execution.
Variables are transient and do not persist after an application stops running.
And, as you’ll read in the “Understanding variable scope and lifetime” sec-
tion, later in this chapter, a variable may last a very short time as the pro-
gram executes or may exist as long as the application is running.
In most cases, you assign a specific data type to each of the variables in your
applications. For example, you may create a string variable to hold text data
such as names or descriptions. A currency variable, on the other hand, is
meant to contain values representing monetary amounts. You shouldn’t try
to assign a text value to a currency variable because a runtime error may
occur as a result.
The variables you use have a dramatic effect on your applications. You have
many options when it comes to establishing and using variables in your
Access programs. Inappropriately using a variable can slow an application’s
execution or potentially cause data loss.
This chapter contains everything you need to know about creating and using
VBA variables. The information in this chapter helps you use the most effi-
cient and effective data types for your variables while avoiding the most
common problems related to VBA variables.