Vbscript Tutorial
Vbscript Tutorial
What is VBScript?
VBScript is a scripting language
A scripting language is a lightweight programming language
VBScript is a light version of Microsoft's programming language Visual Basic
Write text
<html>
<body>
<script type="text/vbscript">
document.write("Hello from VBScript!")
</script>
</body>
</html>
Body section
Execute a script that is placed in the body section. Scripts in the body section are executed when
the page is loading.
<html>
<body>
<script type="text/vbscript">
document.write("Scripts in the body section are executed when the page is loading")
</script>
</body>
</html>
VBScript Variables
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
Array Variables
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"
dim table(4, 6)
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:
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
<html>
<head>
<script type="text/vbscript">
sub mySub()
msgbox("This is a sub procedure")
end sub
</script>
</head>
<body>
<script type="text/vbscript">
call mySub()
</script>
<p>A sub procedure does not return a result.</p>
</body>
</html>
<html>
<head>
<script type="text/vbscript">
function myFunction()
myFunction = "BLUE"
end function
</script>
</head>
<body>
<script type="text/vbscript">
document.write("My favorite color is " & myFunction())
</script>
<p>A function procedure CAN return a result.</p>
</body>
</html>
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
You should use the If...Then...Else statement if you want to
execute some code if a condition is true
select one of two blocks of code to execute
If you want to execute only one statement when a condition is true, you can write the code on
one line:
if i=10 Then msgbox "Hello"
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:
select case payment
case "Cash"
msgbox "You are going to pay cash"
case "Visa"
msgbox "You are going to pay with visa"
case "AmEx"
msgbox "You are going to pay with American Express"
case Else
msgbox "Unknown method of payment"
end select
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.
For Each...Next statement - runs statements for each item in a collection or each element of an
array
Do...Loop statement - loops while or until a condition is true
While...Wend statement - Do not use it - use the Do...Loop statement instead
For...Next Loop
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.
For i=2 To 10 Step 2
some code
Next
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.
For i=10 To 2 Step -2
some code
Next
Exit a For...Next
You can exit a For...Next statement with the Exit For keyword.
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.
Repeating Code While a Condition is True
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.
Repeating Code Until a Condition Becomes True
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
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.