SlideShare a Scribd company logo
VBScript Tutorial
VBScript is a Microsoft scriptinglanguage.
What is VBScript?
 VBScriptisa scriptinglanguage
 A scriptinglanguage isalightweightprogramminglanguage
 VBScriptisa lightversionof Microsoft'sprogramminglanguage Visual Basic
How Does it Work?
When a VBScript is inserted into a HTML document, the Internet browser will read the HTML
and interpret the VBScript. The VBScript can be executed immediately, or at a later event.
Write text
<html>
<body>
<script type="text/vbscript">
document.write("HellofromVBScript!")
</script>
</body>
</html>
VBScript Where To place
Head section
Scripts can be placed in the head section. Usually we put all the "functions" in the head section.
The reason for this is to be sure that the script is loaded before the function is called.
<html>
<head>
<script type="text/vbscript">
alert("Hello")
</script>
</head>
<body>
<p>
We usuallyuse the head sectionfor "functions".
The reason for thisis to be sure that the script is loadedbefore the function is called.
</p>
</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("Scriptsinthe body sectionare executedwhenthe 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.
Rules for Variable Names:
 Must beginwitha letter
 Cannotcontaina period(.)
 Cannotexceed255 characters
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"
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:
 isa seriesof statements,enclosedbythe SubandEnd Substatements
 can performactions,but doesnot return a value
 can take argumentsthat are passedtoit bya callingprocedure
 withoutarguments,mustincludeanemptyset of parentheses()
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
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
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
dim table(4, 6)
<html>
<head>
<script type="text/vbscript">
sub mySub()
msgbox("Thisisa sub procedure")
endsub
</script>
</head>
<body>
<script type="text/vbscript">
call mySub()
</script>
<p>A sub procedure doesnot return a result.</p>
</body>
</html>
<html>
<head>
<script type="text/vbscript">
functionmyFunction()
myFunction = "BLUE"
endfunction
</script>
</head>
<body>
<script type="text/vbscript">
document.write("Myfavorite coloris" & myFunction())
</script>
<p>A functionprocedure CAN return a result.</p>
</body>
</html>
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.
In VBScript we have three conditional statements:
 if statement- use thisstatementif youwantto execute asetof code whena conditionistrue
 if...then...else statement- use thisstatementif youwantto selectone of twosetsof linesto
execute
 if...then...elseif statement- use thisstatementif youwanttoselectone of manysetsof linesto
execute
 selectcase statement- use thisstatementif youwantto selectone of manysetsof linesto
execute
If....Then.....Else
You should use the If...Then...Else statement if you want to
 execute some code if aconditionistrue
 selectone of twoblocks 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.
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.
In VBScript we have four looping statements:
 For...Nextstatement- runsstatementsaspecifiednumberof times.
 For Each...Next statement- runsstatementsforeachitemina collectionoreachelementof an
array
 Do...Loop statement- loopswhile oruntil aconditionistrue
 While...Wendstatement- Donot use it - use the Do...Loopstatementinstead
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
Exita For...Next
You can exit a For...Next statement with the Exit For keyword.
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.
dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
document.write(x & "<br />")
Next
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.
RepeatingCodeWhileaConditionisTrue
You use the While keyword to check a condition in a Do...Loop statement.
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.
RepeatingCodeUntil a ConditionBecomesTrue
You use the Until keyword to check a condition in a Do...Loop statement.
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.
Exita Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword.
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.

More Related Content

What's hot (19)

PPT
Vb script
Aamir Sohail
 
DOC
Learn VbScript -String Functions
Nilanjan Saha
 
PPT
VB Script Overview
Praveen Gorantla
 
PDF
Vbs
santosh_axle
 
PPT
Qtp - Introduction to fundamentals of vbscript
Vibrant Technologies & Computers
 
PPTX
Javascript conditional statements
nobel mujuji
 
PDF
7400354 vbscript-in-qtp
Bharath003
 
DOC
Conditional statements in vb script
Nilanjan Saha
 
PPTX
Conditions In C# C-Sharp
Abid Kohistani
 
PPTX
JavaScript Loop: Optimization of Weak Typing
Janlay Wu
 
PPT
Javascript sivasoft
ch samaram
 
PPTX
Switch case and looping
aprilyyy
 
PPTX
Switch case and looping jam
JamaicaAubreyUnite
 
PPTX
Loops in java script
Ravi Bhadauria
 
PPTX
Loop control statements
Jaya Kumari
 
PPTX
C# Loops
Hock Leng PUAH
 
PPTX
Macasu, gerrell c.
gerrell
 
PDF
C++ Course - Lesson 1
Mohamed Ahmed
 
PPTX
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
Vb script
Aamir Sohail
 
Learn VbScript -String Functions
Nilanjan Saha
 
VB Script Overview
Praveen Gorantla
 
Qtp - Introduction to fundamentals of vbscript
Vibrant Technologies & Computers
 
Javascript conditional statements
nobel mujuji
 
7400354 vbscript-in-qtp
Bharath003
 
Conditional statements in vb script
Nilanjan Saha
 
Conditions In C# C-Sharp
Abid Kohistani
 
JavaScript Loop: Optimization of Weak Typing
Janlay Wu
 
Javascript sivasoft
ch samaram
 
Switch case and looping
aprilyyy
 
Switch case and looping jam
JamaicaAubreyUnite
 
Loops in java script
Ravi Bhadauria
 
Loop control statements
Jaya Kumari
 
C# Loops
Hock Leng PUAH
 
Macasu, gerrell c.
gerrell
 
C++ Course - Lesson 1
Mohamed Ahmed
 
Program control statements in c#
Dr.Neeraj Kumar Pandey
 

Similar to Vb script tutorial (20)

PPTX
Vb script final pari
Kamesh Shekhar Prasad
 
DOCX
VBS control structures for if do whilw.docx
Ramakrishna Reddy Bijjam
 
PPTX
Qtp vb scripting
Bharath Sannadi
 
PPT
Vbscript
VARSHAKUMARI49
 
PPT
30,31,32,33. decision and loop statements in vbscript
VARSHAKUMARI49
 
PPSX
VBScript in Software Testing
Fayis-QA
 
PPT
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
DOCX
VBScript Functions procedures and arrays.docx
Ramakrishna Reddy Bijjam
 
PPTX
CONTROL STRUCTURE IN VB
classall
 
PDF
Vba functions
Wongyu Choe
 
PDF
Conditional Statements & Loops
simmis5
 
PPT
Vb scripting
Brad Winborg
 
PPTX
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
PPT
.Net Controlling Program Flow Statements
BharathiLakshmiAAssi
 
PPTX
Array and functions
Sun Technlogies
 
PPTX
Presentation on visual basic 6 (vb6)
pbarasia
 
PPTX
BSc. III Unit iii VB.NET
Ujwala Junghare
 
PPTX
Looping statements
Jaya Kumari
 
Vb script final pari
Kamesh Shekhar Prasad
 
VBS control structures for if do whilw.docx
Ramakrishna Reddy Bijjam
 
Qtp vb scripting
Bharath Sannadi
 
Vbscript
VARSHAKUMARI49
 
30,31,32,33. decision and loop statements in vbscript
VARSHAKUMARI49
 
VBScript in Software Testing
Fayis-QA
 
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
VBScript Functions procedures and arrays.docx
Ramakrishna Reddy Bijjam
 
CONTROL STRUCTURE IN VB
classall
 
Vba functions
Wongyu Choe
 
Conditional Statements & Loops
simmis5
 
Vb scripting
Brad Winborg
 
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
.Net Controlling Program Flow Statements
BharathiLakshmiAAssi
 
Array and functions
Sun Technlogies
 
Presentation on visual basic 6 (vb6)
pbarasia
 
BSc. III Unit iii VB.NET
Ujwala Junghare
 
Looping statements
Jaya Kumari
 
Ad

More from Abhishek Kesharwani (20)

PDF
Software Engineering unit 1 Notes AKTU ppt
Abhishek Kesharwani
 
PPTX
Software Engineering unit 1 Notes AKTU ppt
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 html
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 html
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 html
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 html
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 html
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 Css
Abhishek Kesharwani
 
PPTX
uptu web technology unit 2 Css
Abhishek Kesharwani
 
PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
PPTX
Unit 1 web technology uptu slide
Abhishek Kesharwani
 
PDF
Unit1 Web Technology UPTU UNIT 1
Abhishek Kesharwani
 
PPTX
Unit1 2
Abhishek Kesharwani
 
PDF
Web Technology UPTU UNIT 1
Abhishek Kesharwani
 
DOCX
Mtech syllabus computer science uptu
Abhishek Kesharwani
 
PDF
Wi max tutorial
Abhishek Kesharwani
 
PDF
Virtual lan
Abhishek Kesharwani
 
Software Engineering unit 1 Notes AKTU ppt
Abhishek Kesharwani
 
Software Engineering unit 1 Notes AKTU ppt
Abhishek Kesharwani
 
uptu web technology unit 2 html
Abhishek Kesharwani
 
uptu web technology unit 2 html
Abhishek Kesharwani
 
uptu web technology unit 2 html
Abhishek Kesharwani
 
uptu web technology unit 2 html
Abhishek Kesharwani
 
uptu web technology unit 2 html
Abhishek Kesharwani
 
uptu web technology unit 2 Css
Abhishek Kesharwani
 
uptu web technology unit 2 Css
Abhishek Kesharwani
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
Unit 1 web technology uptu slide
Abhishek Kesharwani
 
Unit1 Web Technology UPTU UNIT 1
Abhishek Kesharwani
 
Web Technology UPTU UNIT 1
Abhishek Kesharwani
 
Mtech syllabus computer science uptu
Abhishek Kesharwani
 
Wi max tutorial
Abhishek Kesharwani
 
Virtual lan
Abhishek Kesharwani
 
Ad

Vb script tutorial

  • 1. VBScript Tutorial VBScript is a Microsoft scriptinglanguage. What is VBScript?  VBScriptisa scriptinglanguage  A scriptinglanguage isalightweightprogramminglanguage  VBScriptisa lightversionof Microsoft'sprogramminglanguage Visual Basic How Does it Work? When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. Write text <html> <body> <script type="text/vbscript"> document.write("HellofromVBScript!") </script> </body> </html> VBScript Where To place Head section Scripts can be placed in the head section. Usually we put all the "functions" in the head section. The reason for this is to be sure that the script is loaded before the function is called. <html> <head> <script type="text/vbscript"> alert("Hello") </script> </head> <body> <p> We usuallyuse the head sectionfor "functions". The reason for thisis to be sure that the script is loadedbefore the function is called. </p> </body> </html>
  • 2. 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("Scriptsinthe body sectionare executedwhenthe 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. Rules for Variable Names:  Must beginwitha letter  Cannotcontaina period(.)  Cannotexceed255 characters 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"
  • 3. names(2)="Stale" 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:  isa seriesof statements,enclosedbythe SubandEnd Substatements  can performactions,but doesnot return a value  can take argumentsthat are passedtoit bya callingprocedure  withoutarguments,mustincludeanemptyset of parentheses() Sub mysub() some statements End Sub or Sub mysub(argument1,argument2) some statements End Sub 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 Function myfunction() some statements myfunction=some value End Function or Function myfunction(argument1,argument2) some statements myfunction=some value End Function dim table(4, 6)
  • 4. <html> <head> <script type="text/vbscript"> sub mySub() msgbox("Thisisa sub procedure") endsub </script> </head> <body> <script type="text/vbscript"> call mySub() </script> <p>A sub procedure doesnot return a result.</p> </body> </html> <html> <head> <script type="text/vbscript"> functionmyFunction() myFunction = "BLUE" endfunction </script> </head> <body> <script type="text/vbscript"> document.write("Myfavorite coloris" & myFunction()) </script> <p>A functionprocedure CAN return a result.</p> </body> </html> 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. In VBScript we have three conditional statements:  if statement- use thisstatementif youwantto execute asetof code whena conditionistrue  if...then...else statement- use thisstatementif youwantto selectone of twosetsof linesto execute
  • 5.  if...then...elseif statement- use thisstatementif youwanttoselectone of manysetsof linesto execute  selectcase statement- use thisstatementif youwantto selectone of manysetsof linesto execute If....Then.....Else You should use the If...Then...Else statement if you want to  execute some code if aconditionistrue  selectone of twoblocks 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).
  • 6. 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. 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. In VBScript we have four looping statements:  For...Nextstatement- runsstatementsaspecifiednumberof times.
  • 7.  For Each...Next statement- runsstatementsforeachitemina collectionoreachelementof an array  Do...Loop statement- loopswhile oruntil aconditionistrue  While...Wendstatement- Donot use it - use the Do...Loopstatementinstead 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 Exita For...Next You can exit a For...Next statement with the Exit For keyword.
  • 8. 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. dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x in cars document.write(x & "<br />") Next 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. RepeatingCodeWhileaConditionisTrue You use the While keyword to check a condition in a Do...Loop statement. 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. RepeatingCodeUntil a ConditionBecomesTrue You use the Until keyword to check a condition in a Do...Loop statement. Do Until i=10 some code Loop If i equals 10, the code inside the loop will never be executed.
  • 9. 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. Exita Do...Loop You can exit a Do...Loop statement with the Exit Do keyword. 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.