0% found this document useful (0 votes)
11 views52 pages

VB Script

Uploaded by

joe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views52 pages

VB Script

Uploaded by

joe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 52

Visual Basic Scripting

(VBScript)
(Client side scripting)
• Visual Basic Scripting (VBScript) is a subset
of Microsoft Visual Basic used in XHTML
documents to enhance the functionality of a
Web page displayed in a Web browser
• Microsoft’s Internet Explorer Web browser
contains a VBScript scripting engine (i.e., an
interpreter) that executes VBScript code.
• JavaScript has become the de facto client-side
scripting language in industry, hence you are not
likely to use client-side VBScript
NB.
• VBScript is especially valuable when used
with Microsoft Web servers to create
Active Server Pages (ASP), a technology
that allows a server-side script to create
dynamic content that is sent to the client’s
browser
• You may also find the VBScript syntax
similar to JavaScript in some ways
First script
How to Put VBScript Code in an HTML
Document
<html>
<head>
</head>
<body>
<script type="text/vbscript">
document.write("Welcome to vbscript
programming !")
</script>
</body>
</html>
How to Handle Older Browsers
Older browsers that do not support scripts
will display the script as page content. To
prevent them from doing this, you can use
the HTML comment tag:
<script type="text/vbscript">
<!--
some statements
- ->
</script>
VBScript Comments
Two ways you can write a line of comment in VBScript.
VBScript code is commented by using either a
single quote (') or the keyword Rem (for remark) before the comment.
Any text placed after a ' or the keyword Rem is ignored by the interpreter

<SCRIPT LANGUAGE="VBScript">
Rem This line is ignored. I can write it however I want.
Document.Write("2006 FIFA World Cup")
</SCRIPT>
Single quote character followed by the desired text of comment. Here
is an example:
<SCRIPT LANGUAGE="VBScript">
' Anything on this side is part of a comment
Document.Write("They scored two soft goals")
</SCRIPT>
NB No multiline comments
Operators
VBScript is a case-insensitive language
that provides arithmetic operators, logical
operators, concatenation operators,
comparison operators and relational
operators.
Arithmetic operators
Comparison operators
Logical operators
• The VBScript logical operators are And
(logical AND), Or (logical OR), Not (logical
negation), Imp (logical implication), Xor
(exclusive OR) and Eqv (logical
equivalence).
• Note: Despite the mixture of case in
keywords, functions, etc., VBScript is not
case-sensitive—uppercase and lowercase
letters are treated the same, except in
character string constants (also called
character string literals).]
Logical operators
String concatenation
• VBScript provides the plus sign, +, and ampersand, &,
operators for string concatenation, as follows:
• You can use an underscore to indicate a concatenation
(joining two lines together).

Example
<script type="text/vbscript">
document.write("The VBScript syntax allows you to " &_
"concatenate multiple lines together, so that they " &_
"appear joined together seamlessly")
</script>
Data Types
• VBScript has only one data type—Variant—that is
capable of storing different types of data (e.g., strings,
integers, floating-point numbers). All variables are of
type Variant, so the programmer does not
specify a data type when declaring a variable in
VBScript.
• VBScript interprets a Variant in a manner that is suitable
to the type of data it contains. For example, if a Variant
contains numeric information, it will be treated as a
number; if it contains string information, it will be treated
as a string
• The data types (or Variant subtypes) a Variant stores
are listed below
Variant subtypes
VBScript Variables
•Note
•VBScript variables are used to hold values or
expressions.
•In VBScript, all variables are of type variant, that can
store different types of data.
•Variables can be declared simply by using their
name in the VBScript code
•The statement Option Explicit can be used to force
all variables to be declared before they are used.
Forcing all variables to be declared, by using Option
Explicit, can help eliminate various kinds of subtle
errors.
Declaring (Creating) VBScript
Variables
Declare VBScript variables with the Dim,
Public or the Private statement.
Option Explicit
Dim x
Dim carname
You can also declare variables by using its
name in a script.
carname="Volvo"
VBScript Variables
<html>
<body>
<script type="text/vbscript">
Option Explicit
dim name
name="Ravi"
document.write(name)
</script>
NOTE:
1. dim name=“Ravi” –is wrong; both
declaration and initialization cannot be done in
one step.
2. enclose strings in double quotes
Insert a variable value in a text
This example demonstrates how you can insert a variable
value in a text.
<html>
<body>
<script type="text/vbscript">
Option Explicit
dim name
name="Jan Egil"
document.write("My name is: " & name)
</script>

</body>
</html>
VBScript Array Variables
Arrays are data structures consisting of related data items of
the same type.
The Array declaration
In the following example, an array containing 3 elements is
declared:
Dim names(2)
The value 2 defines the upper bound (i.e., the highest valid
index) of names. The lower bound (the lowest valid index) of
numbers is 0
Dim names(2)
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
VBScript Array Variables
Individual array elements are referred to by giving the array
name followed by the element position number in
parentheses, (). The first array element is at position zero.
<html>
<body>
<script type="text/vbscript">
Dim names(2)
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
mother=names(0)
document.write(mother & "<br/>")
document.write(names(2))
</script>
"for loop"
Using a "for loop" to demonstrate how you write the output from an array.
<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)="Borge"
for i=0 to 5
document.write(famname(i) & "<br />")
next
</script>
</body>
</html>
VBScript Procedures
VBScript Function Procedures
We have two kinds of procedures: The Sub procedure
and the Function procedure.
A Sub procedure:
• is a series of statements, enclosed by the Sub
and End Sub statements
• can perform actions, but does not return a value
• can take arguments that are passed to it by a
calling procedure
• without arguments, must include an empty set of
parentheses ()

Note:VBScript’s procedure is the equivalent of a


function in Java-Script
Example (IE Only)
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
Or
Sub mysub()
alert("Hello World")
End Sub
VBScript Built-in Functions
•VBScript provides several predefined functions for example
Variant functions, math functions, functions for interacting with
the user, formatting functions and functions for obtaining
information about the interpreter etc.
•VBScript math functions allow the programmer to perform
common mathematical calculations.
•VBScript provides many functions for manipulating dates and
times. Manipulations include adding dates, subtracting dates,
and parsing dates.
• Consult the VBScript documentation for a list of these
functions.
VBScript Built-in Functions
VBScript provides two functions, InputBox and MsgBox, for
interacting with the user. Function InputBox displays a dialog in which
the user can input data.
For example
intValue = InputBox( "Enter an integer", "Input Box", , _1000, 1000 )
This displays an input dialog containing the prompt ("Enter an
integer") and the caption ("Input Box") at position (1000, 1000) on the
screen.
Function MsgBox displays a message dialog.
For example
Call MsgBox( "VBScript is fun!", , "Results" )
This displays a message dialog containing "VBScript is fun!" with
"Results" in the title bar.
Example
<html>
<head>
<script type="text/vbscript">
Option Explicit
Dim name
sub myName()
name = InputBox("Enter your name", "Enter Name", , 1000, 1000)
document.write("<H1> Hello " & name &" welcome to VbScript class" )
end sub
</script>
</head>
<body>
<script type="text/vbscript">
Call myName()
</script>
</body>
</html>
Example
<html>
<head>
<script type="text/vbscript">
<!--
Option Explicit
Dim intTotal
Sub cmdAdd_OnClick()
Dim intValue
intValue = InputBox("Enter an integer", "Input Box", , 1000, 1000)
intTotal = CInt( intTotal ) + CInt( intValue )
Call MsgBox("You entered " & intValue & "; total so far is " & intTotal, , "Results")
End Sub
-->
</script>
</head>
<body>
Click the button to add an integer to the total.
<hr />
<form action = "">
<input name = "cmdAdd" type = "button"
value = "Click Here to Add to the Total" />
</form>
</body>
</html>
Example(or)
<html>
<head>
<script type="text/vbscript">
<!--
Option Explicit
Dim intTotal
Sub Add()
Dim intValue
intValue = InputBox("Enter an integer", "Input Box", , 1000, 1000)
intTotal = CInt( intTotal ) + CInt( intValue )
Call MsgBox("You entered " & intValue & "; total so far is " & intTotal, , "Results")
End Sub
-->
</script>
</head>
<body>
Click the button to add an integer to the total.
<hr />
<form action = "">
<input name = "cmdAdd" type = "button"
value = "Click Here to Add to the Total" onClick="Add()" />
</form>
</body>
</html>
A Function procedure:
is a series of statements, enclosed by the
Function and End Function statements
• can perform actions and can return a
value
• can take arguments that are passed to it
by a calling procedure
• without arguments, must include an
empty set of parentheses ()
• returns a value by assigning a value to
its name
A Function procedure
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
How to Call a Procedure
There are different ways to call a procedure. You can
call it from within another procedure, on an event, or
call it within a script.
EXERCISE
<html>
<head>
<script type="text/vbscript">
function myFunction()
myFunction = 2*3
end function
</script>
</head>

<body>
<script type="text/vbscript">
document.write("product is " & myFunction())
</script>
<p>A function procedure CAN return a result.</p>
</body>
</html>
Example (IE Only)
<html>
<head>
<title> </title>
</head>
<body>
<script type="text/vbscript">
Function myfunction(a,b)
myfunction=a+b
End Function
document.write(myfunction(5,9))
</script>
</body>
</html>

The function "myfunction" will return the sum of argument "a" and
argument "b".
<html>
Example
<head>
<script type="text/vbscript">
<!--
Option Explicit
Function AddNum(a,b,c)
AddNum= a+b+c ' Return value of the addition of the three arguements
End Function

Sub displayResult(n)
Call MsgBox("The result is "& n, ,"Result")
End Sub

Sub cmdButton_OnClick()
Dim number1, number2, number3, result
' Convert each input to Long subtype
number1 = CLng( Document.Forms( 0 ).txtBox1.Value )
number2 = CLng( Document.Forms( 0 ).txtBox2.Value )
number3 = CLng( Document.Forms( 0 ).txtBox3.Value )
result=AddNum( number1, number2, number3)
Call displayResult(result)
End Sub
-->
</script>
</head>
<body>
<form action = "">
Enter a number <input type = "text" name = "txtBox1" size = "5" value = "0" /><br>
Enter a number <input type = "text" name = "txtBox2" size = "5" value = "0" /><br>
Enter a number<input type = "text" name = "txtBox3" size = "5" value = "0" /><br>
<input type = "button" name = "cmdButton" value="Enter">
</form>
</body>
</html>
Control structures/ Conditional statements
Control structures/ Conditional statements

Conditional statements are used to perform


different actions for different decisions.
In VBScript we have four conditional statements:
•If..Then statement - executes a set of code when
a condition is true
•If...Then...Else statement - select one of two sets
of lines to execute
•If...Then...ElseIf statement - select one of many
sets of lines to execute
•Select Case statement - select one of many sets
of lines to execute
Comparing VBScript and
JavaScript if control structures
Comparing JavaScript’s and
VBScript’s switch statement
Comparing JavaScript’s while to
VBScript’s Do Until
Comparing JavaScript’s do…while
to VBScript’s Do Loop…Until
Comparing JavaScript’s for
to VBScript’s For.
If..Then statement
Execute some code if a condition is true
<html>
<body>
</head>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i >10 Then
document.write("Good morning!")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html>
If...Then...Else
<html>
<head>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i < 10 Then
document.write("Good morning!")
Else
document.write("Have a nice day!")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html>
If...Then...Else If
<html>
<head>
<script type="text/vbscript">
Function greeting()
i=hour(time)
If i = 10 Then
document.write("Just started...!")
ElseIf i = 11 then
document.write("Hungry!")
ElseIf i = 12 then
document.write("Ah, lunch-time!")
ElseIf i = 16 then
document.write("Time to go home!")
Else
document.write("Unknown")
End If
End Function
</script>
</head>
<body onload="greeting()">
</body>
</html>
<html>
Select Case
<body>
<script type="text/vbscript">
d=weekday(date)
Select Case d
Case 1
document.write("Sleepy Sunday")
Case 2
document.write("Monday again!")
Case 3
document.write("Just Tuesday!")
Case 4
document.write("Wednesday!")
Case 5
document.write("Thursday...")
Case 6
document.write("Finally Friday!")
Case else
document.write("Super Saturday!!!!")
End Select
</script>
</body>
</html>
VBScript Looping
Looping Statements
Looping statements are used to run the same
block of code a specified number of times.
In VBScript we have four looping statements:
•For...Next statement - runs code a specified
number of times
•For Each...Next statement - runs code 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
while ...Loop statement instead
For...Next Loop
Use the For...Next statement to run a block of code a
specified number of times.
The For statement specifies the counter variable (i), and its
start and end values. The Next statement increases the
counter variable (i) by one.

<html>
<body>
<script type="text/vbscript">
For i = 0 To 5
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
Looping through headers
This example demonstrates how you can loop
through the 6 headers in html.
<html>
<body>
<script type="text/vbscript">
for i=1 to 6
document.write("<h" & i & ">This is header " & i
& "</h" & i & ">")
next
</script>
</body>
</html>
The Step Keyword
With 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.
<html>
<body>
<script type="text/vbscript">
For i=2 To 10 Step 2
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
Negative Step value
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.
<html>
<body>
<script type="text/vbscript">
For i=10 To 0 Step -2
document.write("The number is " & i & "<br />")
Next
</script>
</body>
</html>
For Each...Next Loop
A For Each...Next loop repeats a block of code for each item in a
collection, or for each element of an array.

<html>
<body>
<script type="text/vbscript">
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x In cars
document.write(x & "<br />")
Next
</script>
</body>
</html>
Do….Loop
The Do...Loop statement repeats a block of code
while a condition is true, or until a condition
becomes true.
<html>
<body>
<script type="text/vbscript">
i=0
Do While i < 10
document.write(i & "<br />")
i=i+1
Loop
</script>
</body>
</html>

You might also like