Declaring Variables

Use the Dim statement in a procedure to declare a variable. Here's the syntax: Dim <VariableName> [As DataType]

Declare the variable's data type in the optional As DataType clause in the Dim statement. A variable's data type determines the kind of information a variable holds, such as String, Integer, or Object. See the "Data Types" section later in this chapter for more information.

Implicit Declaration

You don't have to declare variables as a specific data type before you use them. If you use a variable without explicitly declaring it, Visual Basic creates a variable with that name and assigns it the Variant data type. This is the largest data type available in terms of memory usage and could lead to memory resource problems. When you use an undeclared variable, accidentally misspelling the variable name creates another undeclared variable. These kinds of errors can lead to poor performance and a lot of wasted time debugging your code.

Explicit Declaration

To avoid the hazards mentioned, force Visual Basic to require that you declare all variables before you use them. To do so, place the following line at the top of your code module:

Option Explicit

Alternatively, you can turn on explicit data type checking by choosing Tools > Options and checking the Require Variable Declaration box, as shown in Figure 2-1.

Figure 2-1. Setting explicit data type checking in the Options dialog box

Variable Names

You can name variables almost any way you want, but some restrictions exist. Follow these rules in variable names:

• You must use a letter as the first character.

• You can't use a space; a period (. ); an exclamation mark ( ! ); or the characters &, $, or #

• The name can't be longer than 255 characters.

• Generally, don't use names that are the same as Visual Basic's functions, statements, and methods. To use an intrinsic language function, statement, or method that conflicts with an assigned name, explicitly identify it. Precede the intrinsic function, statement, or method name with the name of the associated type library. For example, if you have a variable called Left, you can invoke the Left function only by using VBA.Left.

• You can't repeat names in the same scope. For example, you can't declare two variables named age in the same procedure. However, you can declare a private variable named age and a procedure-level variable named age in the same module.

■ Note Visual Basic isn't case-sensitive, but it preserves a variable's capitalization in the statement where you declare it.

In addition to restrictions and requirements, there are widely accepted best practices for naming variables to ensure consistency and ease of reading. Begin each variable's name with a prefix that indicates its data type. For example, a String variable named FirstName should be instead named strFirstName. Table 2-1 lists some commonly used prefixes. Not only does this book use this convention but so does most sample code elsewhere.

Table 2-1. Commonly Used Variable Prefixes

Data Type

Prefix

Example

String

str

strFirstName

Label

lbl

lblProperties

Double

dbl

dblXcoordinate

Integer

int

intFloorLevel

ListBox

lst

lstSystemNames

ComboBox

cbo

cboElecSystems

Button

btn

btnChoice

CheckBox

chk

chkSaveBackup

OptionButton

opt

optSaveFormat

Frame

frm

frmSaveOptions

0 0

Responses

  • Mary
    How do i run autocad vba without delcaring variables?
    7 years ago

Post a comment