A variable is a named location in computer memory that you can use for storage of data during the execution of your scripts. You can use variables to: An Introduction to Variables Sub testVariables_OnClick The first line of this example defines a sub procedure associated with the click event of a command button named testVariables. Exactly how, and where, variables are stored is not important. What you use them for, and how you use them is important. Declaring Variables There are two methods for declaring variables in VBScript, explicitly and implicitly. You usually declare variables explicitly with the Dim statement: Dim Name This statement declares the variable Name. You can also declare multiple variables on one line as shown below, although it is preferable to declare each variable separately. Variables can be declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug. You can force VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script. Any variable that is not explicitly declared will then generate an error. Variable Naming Rules Variants and Subtypes Description of Uses for Each Subtype Byte Integer numbers between 0 to 255 Boolean True and False Currency Monetary values Date Date and time Double Extremely large numbers with decimal points Empty The value that a variant holds before being used Error An error number Integer Large integers between -32,768 and 32,767 Long Extremely large integers (-2,147,483,648 and 2,147,483,647) Object Objects Null No valid data Single Large numbers with decimal points String Character strings Assigning Values Syntax: Examples: Scope of Variables The scope of a variable dictates where it can be used in your script. A variable's scope is determined by where it is declared. If it is declared within a procedure, it is referred to as a procedure-level variable and can only be used within that procedure. If it is declared outside of any procedure, it is a script-level variable and can be used throughout the script. <> The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure.
Let's look at a simple VBScript example to clarify the use of variables.
Dim Name
Name = InputBox("Enter your name: ")
MsgBox "The name you entered is " & Name
End Sub
On second line we declare a variable named Name. We are going to use this variable to store the name of the user when it is entered. The third line uses the InputBox function to first prompt for, and then return, the user's name. You will see more of the InputBox function later in this tutorial. The name it returns is stored in the Name variable.
The fourth line uses the MsgBox function to display the user's name. Finally, the sub procedure completes on line five.
VBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The table below describes the subtypes supported by VBScript.
Subtype
You assign a value to a variable by using the following format:
Variable_name = value
Name = "Rahul"
Age=50
The example below demonstrates both script-level and procedure-level variables.
Dim counter
Sub cmdButton_onClick
Dim temp
End Sub
< /SCRIPT >