Declaring Variables

As you saw in Chapter 2, “Introducing Visual Basic,” you use variables to store values while your Visual Basic code is running. Within a procedure or module, you declare a variable with the Dim statement. The syntax for the Dim statement is:

Dim variablename [As type]

In the Dim statement, you supply a name for the variable. Like other things you create and name in Visual Basic, variable names:

You use the optional As type clause in the Dim statement to define the data type of the variable you’re declaring. A data type is a designation of the type of information a variable can store. Examples of data types include String, Integer, and Currency. If you omit the As type clause, Visual Basic makes the variable a Variant, which is the default data type (unless you change the default with the Deftype statement). Data types, including Variant, are discussed later in this chapter. Variables can also contain objects from Microsoft Access, such as Form, Report, and Control objects, or objects from other applications.

See Also   For more information on manipulating objects, see Chapter 5, “Working with Objects and Collections.”

Visual Basic variables are not case-sensitive; when you specify a variable, you don’t need to match the exact uppercase and lowercase letters you used in declaring the variable. Visual Basic automatically changes any explicitly declared variable to the case used in the declaration. An implicitly declared variable is given the same case it had the last time it was used.

Tip   When you name variables, you may find it useful to include prefixes to indicate each variable’s data type. For example, you might name a variable dblTemp to indicate that its data type is Double. This practice makes your code easier to read and helps you avoid type-mismatch errors.

Implicit Declaration

Although it’s not recommended, you can use a variable without explicitly declaring it. For example, you could write a function as follows:

Function SafeSqr(ByVal dblNum As Double) As Double
	dblTemp = Abs(dblNum)
	SafeSqr = Sqr(dblTemp)
End Function

Here, you haven’t declared dblTemp as a variable before you use it in the function. In cases such as this, Visual Basic implicitly creates a variable with that name, so you can use the variable as if you had explicitly declared it. Although implicit declarations are convenient, they can lead to subtle errors in your code if a variable name is misspelled. For example, suppose you wrote the following function:

Function SafeSqr(ByVal dblNum As Double) As Double
	dblTemp = Abs(dblNum)
	SafeSqr = Sqr(dblTmp)				' dblTemp is misspelled.
End Function

At first glance, this looks the same as the previous function. But because the dblTemp variable is misspelled, this function always returns zero. When Visual Basic encounters a new variable name, it can’t determine if you actually meant to create a new variable or if you just made a mistake typing an existing variable name. Because it assumes you want to create a variable, it creates a new variable named dblTmp.

Explicit Declaration

To avoid the problem of misspelling variable names, specify an Option Explicit statement in the Declarations section of your modules. The Option Explicit statement forces the explicit declaration of all variables in a module. If Visual Basic encounters a name not explicitly declared as a variable, it generates an error message.

If you had specified an Option Explicit statement for the module containing the SafeSqr function shown earlier, Visual Basic would have recognized dblTemp and dblTmp as undeclared variables and would have generated errors for both of them. You would then explicitly declare dblTemp, as shown in the following code:

Function SafeSqr(ByVal dblNum As Double) As Double
	Dim dblTemp As Double

	dblTemp = Abs(dblNum)
	SafeSqr = Sqr(dblTmp)
End Function

Now Microsoft Access would display an error message for the incorrectly spelled dblTmp and the problem would be clear. Because the Option Explicit statement helps you catch these kinds of errors, you should use it with all your code.

You can also force variables to be explicitly declared in any new modules by clicking Options (Tools menu), clicking the Module tab, and then selecting the Require Variable Declaration check box. This automatically inserts the Option Explicit statement in any new modules.

Note   The Option Explicit statement operates on a per-module basis; it must be placed in the Declarations section of every form, report, and standard module for which you want Visual Basic to enforce explicit variable declarations. If you select the Require Variable Declaration check box, Visual Basic inserts the Option Explicit statement in all subsequent form, report, and standard modules, but doesn’t add it to existing code. You must manually add the Option Explicit statement to any existing modules within an application.

See Also   For more information on the Option Explicit statement, search the Help index for “Option Explicit statement.”