Variable Names

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Variable names use the mixed-case form (Noun or NounVerb), specifying what the variable is and what it does. The mixed-case form is used as the descriptive portion of the variable name where the first letter of each word is in uppercase and the rest is in lowercase.

Variable names also have a two- or three-character prefix used to specify the variable's data type. For example, the following statements declare variables with a prefix that specifies the variable's data type:

Dim strRecipientName          As String
Dim intItemsProcessed         As Integer
Dim blnContinueProcessing     As Boolean

The two-character prefix is used typically to specify an Office Application object type. For example:

Dim xlApp          As Excel.Application
Dim olNameSpace    As Outlook.NameSpace
Dim wdNewDoc       As Word.Document

Use the "obj" prefix when declaring a generic, or object variable. Use this prefix even when you are creating a late-bound object variable that represents a Microsoft® Office application. For example:

Dim objXLApp       As Object
Dim objWDDocument    As Object
Dim objOLMailItem    As Object

Global and module-level variables use an additional single-character prefix to specify their scope. The scope of a variable defines its lifetime and visibility. Global and module-level variables have a permanent lifetime. That is, the memory allocated to the variable remains allocated as long as the application is running. Variables declared within a procedure are visible only within the procedure where they are declared and have a lifetime that lasts only as long as the code within the procedure is executing. The exception to this is when they are declared by using the Static keyword.

Global variables have a lowercase "g" prefix and are declared in the Declarations section of a module by using the Public statement. They are visible to all procedures in all modules in an application. For example, Public gstrPathToDataSource As String would be a global variable that contains a string that is the path to the data source used in the application.

Variables always should be defined using the smallest scope possible. Use global variables only when there is no other way to share the data they contain. Global variables can make your code hard to understand and difficult to maintain. If you find you are using more than a few carefully chosen global variables, you might want to redesign your code to eliminate them.

Module-level variables have a lowercase "m" prefix and are declared in the Declarations section of a module by using the Dim or Private statement. They are visible to any procedure within the module in which they are declared. For example, Dim mrstCustomerRecords As ADODB.Recordset would be a module-level object variable for customer records. In class modules, module-level variables that are declared with the Private statement have a "p_" prefix. Public module-level variables in class modules appear as properties of the class and should not have any prefix to indicate their data type or scope.

Procedure-level variables are created within a procedure by using the Dim statement. For example, Dim intCurrentMailItem As Integer would be a procedure-level variable used as a loop counter. In addition, procedure-level variables can be declared by using the Static keyword. Static variables retain their value even after the procedure in which they are declared has finished running. Static procedure-level variables have a lowercase "s" prefix. For example, Static scurTotalSales As Currency would create a procedure-level static variable used to keep an accumulating total in a procedure that calculates current sales.

User-defined type variables are declared in the Declarations section of a module by using an all uppercase type name with "_TYPE" appended to the type name. You could declare a user-defined type in the following manner:

Type EMPLOYEEINFO_TYPE
   strFullName As String
   lngEmployeeID As Long
   datStartDate As Date
   strDepartmentCode As String * 4
   curSalary As Currency
End Type

You declare a module-level variable of type EMPLOYEEINFO_TYPE by using a "udt" prefix. For example, Dim mudtEmployeeRecord As EMPLOYEEINFO_TYPE.

Array variables have a lowercase "a" prefix and, unless the variable is a variant, are followed by a pair of parentheses. An array is a variable that can contain multiple values. Array variables are declared by using the Dim statement; for example, Dim alngNum() is an array variable of type Long. Arrays are useful when you must store a number of values of the same type, but you do not want to create individual variables to store them all.

The following are some examples of variable names that use the general naming guidelines described earlier.

Variable Data type Scope
strCompanyName String Procedure
rstCurrentOrders Object Procedure
intCurrentRecordCount Integer Procedure
wdWordApplication Object Procedure
varClipboardData Variant Procedure
curAmountPastDue Currency Procedure
blnProcessNextRecord Boolean Procedure
molOutlookMailItem Object Module
mcolCurrentUsers Object Module
gcnnDBConnection Object Global
gstrLogonID String Global
gastrContactNames() String (array) Global
molOutlookMailItem Object Module
mcolCurrentUsers Object Module
gcnnDBConnection Object Global
gstrLogonID String Global
gastrContactNames() String (array) Global

See Also

Using a Naming Convention | Naming Variables and Constants | Constant Names | Custom Classes and Objects | Getting the Most Out of Visual Basic for Applications