Variables, Declarations, and Scope


Variable Declarations

Type Description
Dim When this statement is used to declare a variable within a procedure, the variable is only recognized within the procedure. Variables declare with Dim at the form level are available for use by any procedure within the form
Public Variables declared with this statement may be used by all procedures in all modules in a project
Private This statement is used at the top of a module to declare variables that can only be accessed from within that module
ReDim This statement is used to declare dynamic-array variables
Static Variables are declared at the procedure level with this statement. Its use is the same as procedure-level Dim statement except that Static variables retain their values only as long as the procedure in which they are declare is running

Term Description
Variable
  • A temporary storage location residing in memory. It is capable of being modified during program execution. 
Rules
  • Max 255 characters beginning with alpha 
  • Cannot have period as "." because it is used to separate properties. 
  • Cannot be a reserved word
Conventions
  • More than one variable can be declared together
  • All variables and constants must be declared at the top of the module in which they are used.
  • Every variable declaration should include an inline comment describing the use of the variable being declared.

Data Types

Data Type Storage Size Description (range)
Boolean 2 bytes either True or False
Integer 2 bytes -32,768 to 32,768
Long (integer) 4 bytes -2,147,483,648 to 2,147,483,647
Single (precision/non-integer) 4 bytes -3.40E38 to 3.40E38
Double (precision/non-integer) 8 bytes -1.80E308 to 1.80E308
Currency 8 bytes -9.22E14 to 9.22E14
String 1 byte/character This type consists of strings of characters. When strings are used on code, they must be contained within quotation marks. 
Variant Variant variables can adjust automatically to store any type of data, and type conversions can be made easily.
Date 8 bytes A Date-type datum requires eight bytes of storage and ranges from January 1, 100 to December 31, 9999
Byte 1 byte.datum This data type consists of integers in the range from 0 to 255.

Scope of Variable Declarations

Depending where the variable is declared, it may be accessible only to specific objects, forms or global to the entire project.


Scope Description
Local Variables can be declared within each object. For example during the execution of a command button you may need a temporary storage area. It is not available beyond that procedure. Use Dim
Form Level Variables declared in the general declarations of a given form are available in every object within that form.  Use Private
Global Variables declared in the general declarations of a module are available throughout the project, in every form.  Use Public
The following shows the relationship between the different types of variables
Form A 
Private Xaxis as Long
 
Procedure A
DIM Age as Integer
Form B 
Private Yaxis as integer
 
 Procedure B
DIM Name as String
Module A
General Declarations of Code Module
Choose Project, Add Module 
Public Cost as Currency 

Mathematical Operators

In order to compute inputs from users and to generate results, we need to use various mathematical operators. In Visual Basic, except for + and -,  the symbols for the operators are different from normal mathematical operators, as shown in the table below.


Operator Mathematical function Example
^ Exponential 2^4=16
* Multiplication 4*3=12
/ Division 12/4=3
Mod Modulus (return the remainder from an integer division) 15 Mod 4=3
\ Integer Division (discards the decimal places) 19\4=4
+ or & String concatenation "Visual"&"Basic"="VisualBasic"

Comparison Operators

The comparison expression can be any expression at all, but it usually contains one of the relational expressions. If the expression has the value 0, it is considered false, and the statement is skipped.


Relational Expression Meaning
equal to
< less than
< = less than or equal
greater than
> = greater than or equal
< > not equal to

Mathematical Hierarchy

Mathematical Term Operation Boolean Logic
Brackets  ( )  NOT 
Exponents   
Multiplication  AND 
Division   
Addition  OR 
Subtraction   

Variables and Constants in Multiple-Form Projects

    When you have multiple forms in a project, the scope of the variables and constants becomes a little more complicated. The variables and constants can be local to a procedure, module level, which are available to all procedures in the module; or global, which are available to all procedures in all modules in the project.

Global Variables and Constants

    To make variables and constants accessible to more than one form in the projecy, they must be global variables. To declare global variables, use the Public statement in place of the Dim statement.

Public Identifier (As DataType)
Public Const Identifier (As DataType) = Value

    The format of the Public statement is similar to the Dim and Const statements. If the data type is omitted for a variable, the type defaults to variant.
    It is important to know the scope of a variable when programming. A module-level variable or constant has a prefix of "m", and a public variable or constant has a prefix of "g" (for global; "p" refers to private). Any variable or constant without a scope prefix is assumed to be local to a procedure.

Public gcGrandTotal As Currency
Public gPersonName 'Defaults to Variant data type
Public Const gsTAX_RATE As Single = .0825
Public Const gstCOMPANY -  "R 'n R - Reading"

Static Variables

    Another statement you can use to declare variables at the procedure level is the Static statement. Static variables retain their value for the life of the project, rather than being reinitialized for each call to a procedure. If you need to retain the value in a variable for muliple calls to a procedure, such as running total, declare it as Static.  Static statements can only appear in procedures. Static statements never appear in the General Declarations section of a module. They do not require a scope prefix, since all static variables are local.

Static Identifier (As DataType)

Examples,

Static iPersonCount As Integer
Static cReportTotal As Currency