VariabLEs and BEGinninG Data Types

Paramount in any programming language is the concept of variables. In a nutshell, variables are pointers to storage locations in memory that contain data. You often hear variables referred to as containers for data. In reality they are pointers that represent a memory address pointing to a memory location.

Though every variable created is unique (unique memory address), all variables share some common characteristics:

• Every variable has a name.

• Each variable has an associated memory address (hidden in high-level languages such as VBA).

• Variables have a data type such as String, Integer, or Boolean.

Variables in Access VBA must begin with a letter and cannot be longer than 255 characters, nor can they contain periods or spaces. When created, variable names point to a location in memory that can be managed during the execution of your program.

Demonstrated next, VBA programmers use the Dim keyword (short for dimension) to declare a new variable in what's called a declaration statement:

Dim myVariable

Once a variable has been declared, VBA reserves space in memory so you can store and retrieve data from its memory location using VBA statements. Simply declaring variables is not the end of the road. It is good programming practice to tell VBA what kind of variable, the data type, you are creating. When creating variables, you should ask yourself whether your variable stores strings, numbers, Boolean, dates, or object type data.

VBA provides a number of data types for declaring variables. The more common are listed in Table 2.1.

Table 2.1 Common Data Types in VBA

Data Type

Boolean Currency

Date Double

Integer

Long

Single

Storage Size

2 bytes 8 bytes

8 bytes 8 bytes

2 bytes 4 bytes 4 bytes

0 0

Post a comment