The syntax of VBScript is identical to that of Visual Basic for Applications. However, VBScript is designed to download quickly over the Internet, so many language and run-time elements supported by Visual Basic for Applications are not supported under VBScript. Also, some language elements, such as file I/0, have been removed for security purposes.
Example
In this VBScript example, a message box is displayed if the variable "Var" is numeric:If IsNumeric(Var) Then MsgBox "Var is a number." End If
In addition, some functions are implemented differently in VBScript. The Visual Basic Format function can be called in one of four specific versions in VBScript:
NoteFor a complete reference of all functions that are supported by VBScript 2.0, refer to the VBScript Web site (www.microsoft.com/vbscript) or the Internet Client SDK.
Sub Procedures
A sub procedure is a series of VBScript statements that are enclosed by the Sub and End Sub statements. It performs actions but does not return a value. A sub procedure can take arguments (constants, variables , or expressions that are passed by a calling procedure).Example
In this example, a sub procedure is used to increment the sales variable by the value passed in NumSales :Sub IncreaseSales(NumSales) Sales = Sales + NumSales End Sub
To call a sub procedure from another procedure, type the name of the procedure along with values for any required arguments, separating each with a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses:
IncreaseSales 100 -or- Call IncreaseSales(100)
Function Procedures
A function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A function procedure is similar to a sub procedure, but can return a value. A function procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a function procedure has no arguments, its Function statement must include an empty set of parentheses. A function procedure returns a value by assigning a value to its name in one or more statements. The return type of a function procedure is always a Variant data type.Example
This example declares a function procedure that verifies a date value. An example of how to call this function is then shown:Function Validate(myDate) If IsDate(myDate) Then Validate = True Else Validate = False End If End Function 'Event procedure called when user clicks button named cmdTest Sub cmdTestDate_OnClick() Results = Validate(myDate) End Sub* To create a Web in Microsoft FrontPage 98
Function SquareIt(Num) SquareIt = (Num * Num) End Function
Dim Result On Error Resume Next Result = SquareIt(T1.Value) MsgBox T1.Value & " squared = " & Result
The number you typed into the text box will be processed by the function you created (SquareIt). The result is then displayed in a message box.
Data Types
VBScript supports only the Variant data type, which can hold different types of data, such as strings or numbers . In general, you can store the data you need in a Variant data type and the data will function appropriately.To determine the type of data currently in the variable, you can use the VarType function.
To declare a variable in VBScript, you use the Dim statement.
Example
This example declares the variable myName :Dim myNameArrays VBScript also supports arrays. All arrays in VBScript are zero-based . The first index number in an array is always zero.
Example
This example declares an array that can store 11 values:Dim Students(10)Constants To create a constant in VBScript, use the Const statement. You can create string or numeric constants with meaningful names , and then assign literal values to them.
Example
This example uses the Const statement to declare two constants and assigns values to them:Const MYSTRING = "This is my string" Const MYAGE = 37
NoteUse all uppercase letters when creating constants to differentiate them from variables.
If you declare a variable in a procedure, it has local scope. The variable is only available to the procedure. When the procedure terminates, the variable goes out of scope.
If you declare a variable outside of a procedure, it is available to all procedures on the Web page. This is referred to as script-level scope. A script-level variable maintains its value while the Web page is displayed in the browser.
A variable may be declared as either Public or Private when declared at script-level. Variables declared using the Public statement are available to all procedures in all scripts. However, Private variables are available only to the script in which they are declared. Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure.
You can also use the Dim , Public , or Private statements with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure 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 Private , Public , or Dim statement, an error occurs.
Syntax
The following syntax is used when declaring variables:Dim varname [([subscripts])] Public varname [([subscripts])] Private varname [([subscripts])]Example In this example, the variable X is available to all scripts and any corresponding procedures in the project. The Y variable, however, is available to the current script. Variable Z has scope only within the sub procedure MySub :
<SCRIPT> Public X Private Y Sub MySub() Dim Z End Sub </SCRIPT>
NoteWhen declaring variables, the Dim , Public , and Private statements in a procedure are generally used at the beginning of the procedure.
* To use variables in VBScript
Option Explicit Private Y Sub SetValues() Dim X X = 1 Y = 1 End Sub Sub ChangeValuesAgain() X = 1 Y = 1 End Sub
SetValues ChangeValuesAgain
Notice that you receive an error stating that the variable X is undefined. This is because X is only valid within the SetValues subroutine where it was declared using the Dim statement. However, the variable Y is in scope because it was declared as Private outside of a procedure.
Dim X
Notice that the error no longer occurs because X is now in scope due to the Dim statement that you just added in the ChangeValuesAgain subroutine.
VBScript supports most of the Visual Basic for Applications structures for controlling program flow.
Looping Structures
Looping structures allow a block of script to run more than one time based on the condition of a variable or user input. The following looping statements are available in VBScript 2.0.Statement | Description |
Do Loop | Loops while or until a condition is True. |
While Wend | Loops while a condition is True. |
For Next | Uses a counter to run statements a specified number of times. |
Example
This example creates an array and fills it with values:Dim x(10) For i = 1 to 10 x(i) = i * 10 Next
Conditional Branching
Conditional branching controls if and when a certain block of script executes. This is dependent on the condition of variables or user input. Conditional branching differs from looping structures in that script in a conditional branch runs only once. The following conditional branching statements are available in VBScript 2.0.Statement | Description |
If Then | Execute code based on a whether a statement is true or false. |
Select Case | Execute code based on the value of a variable. |
You can use the If ... Then ... Else statement to evaluate whether a condition is True or False and, depending on the result, specify one or more statements to run.
Example
This If ... Then ... Else statement calculates a bonus percentage based on the value of the Sales variable:If Sales > 100000 Then Bonus = .10 ElseIf Sales > 50000 Then Bonus = .05 Else Bonus = .02 End If
VBScript 2.0 now supports the use of the Select Case statement. Select Case statements are useful when the variable being interrogated could contain more than two different values. Although your code can use embedded If Then statements, Select Case is typically easier to read and maintain.
Example
This example uses the variable Sales to determine the appropriate bonus:Select Case Sales Case 100000 Bonus = .1 Case 50000, 75000 Bonus = .05 Case Else Bonus = .02 End Select
NoteSelect Case statements do not support ranges of values; they also do not support use of the greater than (>) or less than (<) signs.
* To use the Select Case construct in VBScript
Dim Result On Error Resume Next Result = SquareIt(T1.Value) Select Case Result Case 1,2,3 MsgBox "Between 1 and 3" Case 4,5,6 MsgBox "Between 4 and 6" Case 7,8,9 MsgBox "Between 7 and 9" Case Else MsgBox "Result is out of range." End Select MsgBox T1.Value & " squared = " & Result
Notice the response for entering a number between one and three. Entering any other number will cause the code under the Case Else statement to execute.