Variables

Variables are used to hold pieces of information which can change during the operation of a Script. They can hold either:

  • Alpha-numeric character strings

    or

  • Integers

 

A variable can store a single value or an array of values.

Before you can use a variable within your Script, you must declare it. This simply means that you must tell the Script about it. You do this using a 'Dim' statement.

For a single value variable, the syntax is:

Dim <variablename>

For an array variable, the syntax is:

Dim <variablename>(n)

where n is the upper bound of the array and the size of the array is n+1. For example:

Dim name(5)

would declare an array of 6 'names'.

 

You can name a variable anything you like, except the name can't start with a number or contain a full stop nor can you use the reserved words, i.e. the Event or Method names and words used in the Script statements, such as 'Dim', 'If', 'Else' etc.

 

Once you have declared a variable, you can add a value to it using the '=' operator, for example:

FirstName = "Ann"

RecNum = 0

Num1 = -1

Rtn = MyFunction(FirstName, Surname)

Totl = Number1 + Number2 '("+" is an arithmetic Operator - see Operators topic for more information)

FullName = Title + " " + FirstName + " " + Surname

Col1 = Null

or if using an array variable:

Name(3) = "John" 'sets the fourth element in the name array to "John"

Name(n) = "Alex" 'sets the (n+1)th element in the name array to "Alex"

FirstName = Name(0) 'sets the FirstName variable to the contents of the first element in the names array

 

You can declare a variable and assign a value to it on the same line if you want to, for example:

Dim Total = 0

Dim Rtn = MyFunction(Param1, Param2)

Array variables, however, must be declared and then assigned values separately, e.g. within a For … Next loop; you cannot assign values to an array within the Dim statement.

 

Variables can be limited to use within a specific Function or can be 'global', meaning they are available throughout the whole app. If you declare a variable within a Function, its use is limited to within that Function. If you declare it outside of any Function, then it will be global.

For example:

Dim FirstName

Dim Surname

 

Function OnLoad()

Dim Rtn

[code statements]

Rtn = MyFunction(FirstName, Surname)

MyFunction2()

[code statements]

End Function

In this example, the two variables FirstName and Surname are now global variables, but the Rtn variable can only be used within the OnLoad Function.

    • Variables don't exist until the Script which contains them has been executed for the first time. Consequently, you may want to declare all global variables in the application OnLoad Event, so they are created as soon as the app is loaded.

    • Global variables may be declared multiple times in different Scripts - only the first is actioned and subsequent declarations are ignored.

    • Variable names are not case-sensitive.

    • You don't have to specify whether the variable will hold numeric or alpha-numeric data, this is dealt with automatically.

    • String values must be enclosed in double quotes. To include quotation marks within a string either use "" (e.g. "Please enter your ""Username""") or use the Chr Scripting Method.