Professional Documents
Culture Documents
VBScript Variables
What is a Variable?
A variable is a "container" for information you want to store. A variable's value can change during the
script. You can refer to a variable by name to see its value or to change its value. In VBScript, all variables
are of type variant, that can store different types of data.
Declaring Variables
You can declare variables with the Dim, Public or the Private statement. Like this:
dim name
name=some value
Now you have created a variable. The name of the variable is "name".
You can also declare variables by using its name in your script. Like this:
name=some value
Now you have also created a variable. The name of the variable is "name".
However, the last method is not a good practice, because you can misspell the variable name later in your
script, and that can cause strange results when your script is running. This is because when you misspell
for example the "name" variable to "nime" the script will automatically create a new variable called
"nime". To prevent your script from doing this you can use the Option Explicit statement. When you use
this statement you will have to declare all your variables with the dim, public or private statement. Put the
Option Explicit statement on the top of your script. Like this:
option explicit
dim name
name=some value
name="Hege"
i=200
The variable name is on the left side of the expression and the value you want to assign to the variable is
on the right. Now the variable "name" has the value "Hege".
Lifetime of Variables
How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be accessed within that procedure.
When the procedure exits, the variable is destroyed. These variables are called local variables. You can
have local variables with the same name in different procedures, because each is recognized only by the
procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of
these variables starts when they are declared, and ends when the page is closed.
Array Variables
Sometimes you want to assign more than one value to a single variable. Then you can create a variable
that can contain a series of values. This is called an array variable. The declaration of an array variable
uses parentheses ( ) following the variable name. In the following example, an array containing 3 elements
is declared:
dim names(2)
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a
fixed-size array. You assign data to each of the elements of the array like this:
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
Similarly, the data can be retrieved from any element using the index of the particular array element you
want. Like this:
mother=names(0)
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers
in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7
columns:
dim table(4, 6)
Note that the following VBScript runs on the client side, which is different from ASP.
<html>
<body>
<script type="text/vbscript">
dim famname(5)
famname(0)="Jan Egil"
famname(1)="Tove"
famname(2)="Hege"
famname(3)="Stale"
famname(4)="Kai Jim"
famname(5)=99+2
for i=0 to 5
document.write(famname(i) & "<br>")
next
document.write("<br><br>")
dim v(1,1)
v(0,0) = "Daniel"
v(0,1) = "Ben"
v(1,0) = "Roger"
v(1,1) = 25
</script>
</body>
</html>
VBScript Functions
VBScript Procedures
We have two kinds of procedures: The Sub procedure and the Function procedure.
A Sub procedure:
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
A Function procedure:
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
name = findname()
Here you call a Function called "findname", the Function returns a value that will be stored in the variable
"name".
Here you also call a Function called "findname", the Function returns a value that will be displayed in the
message box.
When you call a Sub procedure you can use the Call statement, like this:
Call MyProc(argument)
MyProc argument
VBScript Conditional Statements
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use
conditional statements in your code to do this.
• if...then...else statement - use this statement if you want to select one of two sets of lines to
execute
• if...then...elseif statement - use this statement if you want to select one of many sets of lines to
execute
• select case statement - use this statement if you want to select one of many sets of lines to
execute
If....Then.....Else
If you want to execute only one statement when a condition is true, you can write the code on one line:
There is no ..else.. in this syntax. You just tell the code to perform one action if the condition is true (in
this case if i=10).
If you want to execute more than one statement when a condition is true, you must put each statement
on separate lines and end the statement with the keyword "End If":
if i=10 Then
msgbox "Hello"
i = i+1
end If
There is no ..else.. in this syntax either. You just tell the code to perform multiple actions if the condition
is true.
If you want to execute a statement if a condition is true and execute another statement if the condition is
not true, you must add the "Else" keyword:
if i=10 then
msgbox "Hello"
else
msgbox "Goodbye"
end If
The first block of code will be executed if the condition is true, and the other block will be executed
otherwise (if i is not equal to 10).
If....Then.....Elseif
You can use the if...then...elseif statement if you want to select one of many blocks of code to execute:
if payment="Cash" then
msgbox "You are going to pay cash!"
elseif payment="Visa" then
msgbox "You are going to pay with visa."
elseif payment="AmEx" then
msgbox "You are going to pay with American Express."
else
msgbox "Unknown method of payment."
end If
Select Case
You can also use the SELECT statement if you want to select one of many blocks of code to execute:
This is how it works: First we have a single expression (most often a variable), that is evaluated once. The
value of the expression is then compared with the values for each Case in the structure. If there is a match,
the block of code associated with that Case is executed.
VBScript Looping Statements
Looping Statements
Very often when you write code, you want to allow the same block of code to run a number of times. You
can use looping statements in your code to do this.
For...Next
You can use a For...Next statement to run a block of code, when you know how many repetitions you
want.
You can use a counter variable that increases or decreases with each repetition of the loop, like this:
For i=1 to 10
some code
Next
The For statement specifies the counter variable (i) and its start and end values. The Next statement
increases the counter variable (i) by one.
Step Keyword
Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
In the example below, the counter variable (i) is increased by two each time the loop repeats.
To decrease the counter variable, you must use a negative Step value. You must specify an end value that
is less than the start value.
In the example below, the counter variable (i) is decreased by two each time the loop repeats.
Exit a For...Next
You can exit a For...Next statement with the Exit For keyword.
For Each...Next
A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an
array.
The For Each...Next statement looks almost identical to the For...Next statement. The difference is that
you do not have to specify the number of items you want to loop through.
dim names(2)
names(0)="Tove"
names(1)="Jani"
names(2)="Hege"
Do...Loop
You can use Do...Loop statements to run a block of code when you do not know how many repetitions you
want. The block of code is repeated while a condition is true or until a condition becomes true.
Do While i>10
some code
Loop
If i equals 9, the code inside the loop above will never be executed.
Do
some code
Loop While i>10
The code inside this loop will be executed at least one time, even if i is less than 10.
Do Until i=10
some code
Loop
If i equals 10, the code inside the loop will never be executed.
Do
some code
Loop Until i=10
The code inside this loop will be executed at least one time, even if i is equal to 10.
Exit a Do...Loop
Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop
The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than
10.
VBScript Keywords
VBScript Keywords
Keyword Description
Empty Used to indicate an uninitialized variable value. A variable value is
uninitialized when it is first created and no value is assigned to it, or
when a variable value is explicitly set to empty.
Conversion Functions
Function Description
Asc Converts the first letter in a string to ANSI code
CBool Converts an expression to a variant of subtype Boolean
CByte Converts an expression to a variant of subtype Byte
CCur Converts an expression to a variant of subtype Currency
CDate Converts a valid date and time expression to the variant of subtype Date
CDbl Converts an expression to a variant of subtype Double
Chr Converts the specified ANSI code to a character
CInt Converts an expression to a variant of subtype Integer
CLng Converts an expression to a variant of subtype Long
CSng Converts an expression to a variant of subtype Single
CStr Converts an expression to a variant of subtype String
Hex Returns the hexadecimal value of a specified number
Oct Returns the octal value of a specified number
Syntax
CInt(expression)
Parameter Description
expression Required. Any valid expression
Example 1
dim a
a=134.345
document.write(CInt(a))
Output:
134
Example 2
dim a
a=-30000.24
document.write(CInt(a))
Output:
-30000
Math Functions
Function Description
Abs Returns the absolute value of a specified number
Atn Returns the arctangent of a specified number
Cos Returns the cosine of a specified number (angle)
Exp Returns e raised to a power
Hex Returns the hexadecimal value of a specified number
Int Returns the integer part of a specified number
Fix Returns the integer part of a specified number
Log Returns the natural logarithm of a specified number
Oct Returns the octal value of a specified number
Rnd Returns a random number less than 1 but greater or equal to 0
Sgn Returns an integer that indicates the sign of a specified number
Sin Returns the sine of a specified number (angle)
Sqr Returns the square root of a specified number
Tan Returns the tangent of a specified number (angle)
The Rnd function returns a random number. The number is always less than 1 but greater or equal to 0.
Syntax
Rnd[(number)]
Parameter Description
number Optional. A valid numeric expression
If number is:
Example 1
document.write(Rnd)
Output:
0.7055475
Example 2
String Functions
Function Description
InStr Returns the position of the first occurrence of one string within another.
The search begins at the first character of the string
InStrRev Returns the position of the first occurrence of one string within another.
The search begins at the last character of the string
LCase Converts a specified string to lowercase
Left Returns a specified number of characters from the left side of a string
Len Returns the number of characters in a string
LTrim Removes spaces on the left side of a string
RTrim Removes spaces on the right side of a string
Trim Removes spaces on both the left and the right side of a string
Mid Returns a specified number of characters from a string
Replace Replaces a specified part of a string with another string a specified
number of times
Right Returns a specified number of characters from the right side of a string
Space Returns a string that consists of a specified number of spaces
StrComp Compares two strings and returns a value that represents the result of
the comparison
String Returns a string that contains a repeating character of a specified length
StrReverse Reverses a string
UCase Converts a specified string to uppercase
The StrComp function compares two strings and returns a value that represents the result of the
comparison.
Syntax
StrComp(string1,string2[,compare])
Parameter Description
string1 Required. A string expression
string2 Required. A string expression
compare Optional. Specifies the string comparison to use. Default is 0
Example 1
document.write(StrComp("VBScript","VBScript"))
Output:
0
Example 2
document.write(StrComp("VBScript","vbscript"))
Output:
-1
Example 3
document.write(StrComp("VBScript","vbscript",1))
Output:
0
For more function references, you can check the reference materials from
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp.