See Also Example
Used in the Declarations section of a module to declare global variables and allocate storage space. Global variables are available to all procedures in all forms and modules.
Global varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type] ] . . .
The Global statement has these parts:
varname Name of a variable.
subscripts Dimensions of an array . You can declare multiple dimensions. The syntax of subscripts is described below.
As type Reserved word used to declare the data type of a variable. The type may be Integer, Long, Single, Double, Currency, String (for variable-length strings), String * length (for fixed-length strings), Variant , a user-defined type, or an object type . Use a separate As type clause for each variable being defined.
New Creates a new instance of a specific object type, such as Form1. The New reserved word cannot be used to create new variables of any of the fundamental data types, nor can it be used to create a variable of any generic object type (MDIForm, Form or Control.) or any specific control type (CommandButton, TextBox, and so on).
The argument subscripts has the following syntax:
[lower To ]upper[,[lower To] upper] . . .
The To reserved word provides a way to indicate both the lower and upper bounds of an array variable's subscripts. The following statements are equivalent if there is no Option Base statement:
Global A(0 To 8, 0 To 3)
Global A(8, 0 To 3)
Array subscripts can be negative. To can be used to specify any range of subscripts between -32,768 and 32,767, inclusive. For example:
Global A(-4 To 10)
Global B(-99 To -5, -3 To 0)
The maximum number of array dimensions allowed in a Global statement is 60.
If you use a subscript that is greater than the specified maximum or smaller than the specified minimum, an error occurs.
Use the Global statement in the Declarations section of a module to declare variables that are available to all procedures in all modules. You can also use the Global statement to declare the data type of a variable. For example, the following statement declares the variable as an Integer:
Global NumberOfEmployees As Integer
The following statement declares a variable for a new instance of a form:
Global X As New Form1
If New is not used when declaring an object variable, no instance of the object actually exists. The variable must be assigned to an existing object of type using the Set statement before it can be used. Until it is assigned to an existing object, the declared object varaible has the special value Nothing, which indicates that it does not refer to any particular instance of an object.
You can also use the Global statement with empty parentheses to declare dynamic arrays. After declaring a dynamic array, use the ReDim statement within a procedure level to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Global or Dim statement, an error occurs.
Note You cannot use the Global statement in forms, because form variables are available only to procedures within the form where they are declared.
When variables are initialized, numeric variables are initialized to 0, Variant variables to Empty Variable-length strings are initialized as zero-length strings, and fixed-length strings are filled with zeros. The fields of user-defined type variables are initialized as if they were separate variables..
Option Base Statement
Global Statement Example
The example uses the Global statement to declare two variables in a module. The first is a global dynamic-array variable; the second is a global integer variable. To try this example, paste the Form_Click procedure into the Declarations section of a form containing a list box named List1. Paste the remaining code into the Declarations section of a module. Then press F5 and click the form.
Sub Form_Click ()
LoadGlobalArray ' Call GlobalDemo procedure.
For I = 1 To UBound(TestArray) ' Determine array size and
List1.AddItem TestArray(I) ' put array items in list box.
Global TestArray() ' Declare dynamic-array.
Global Size As Integer ' Declare integer.
Sub LoadGlobalArray ()
Dim I ' Declare local variables.
Size = Int(100 * Rnd + 1) ' Generate random size.
ReDim TestArray(Size) ' Define how many elements.
For I = 1 to Size ' Index of elements.
TestArray(I) = Int(100 * Rnd + 1) ' Put random number in each element.