VBScript
Part 1
Lecture 6
Sara Almudauh
Development of Internet Application
1501CT - Sara Almudauh
VBScript is a Microsoft scripting language.
VBScript is a light version of Microsoft's programming language
Visual Basic.
VBScript is the default scripting language in ASP (Active Server
Pages).
Development of Internet Application
1501CT - Sara Almudauh
VBScript on a Server
• When VBScript is used with ASP, the statement response.write() produces
output.
<html>
<body>
<%response.write("This is my first VBScript")%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
VBScript Variables
• A variable can have a short name, like x, or a more descriptive
name, like carname.
• Rules for VBScript variable names:
• Must begin with a letter
• Cannot contain a period (.)
• Cannot exceed 255 characters
• In VBScript, all variables are of type variant, that can store
different types of data.
Development of Internet Application
1501CT - Sara Almudauh
Declaring (Creating) VBScript Variables
• You can declare VBScript variables with the Dim, Public or the
Private statement. Like this:
• Dim x
Dim carname
Development of Internet Application
1501CT - Sara Almudauh
Example
• <!DOCTYPE html>
<html>
<body>
<%
Dim firstname
firstname=“Sara"
response.write(firstname)
response.write("<br>")
firstname=“Ahmed"
response.write(firstname)
%>
Sara
Ahmed
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
Example
• <!DOCTYPE html>
<html>
<body>
<%
Dim firstname
firstname=“Sara”
• response.write(“your first name is :”&firstname)
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
your first name is : Sara
VBScript Procedures
• VBScript has two kinds procedures:
1. Sub procedure
2. Function procedure
Development of Internet Application
1501CT - Sara Almudauh
Sub Procedures
• A Sub procedure:
1. is a series of statements, enclosed by the Sub and End
Sub statements
2. can perform actions, but does not return a value
3. can take arguments
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
Development of Internet Application
1501CT - Sara Almudauh
Example
• <!DOCTYPE html>
<html>
<body>
<%
Sub mysub()
response.write("I was written by a sub
procedure")
End Sub
response.write("I was written by the script<br>")
Call mysub()
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
I was written by the script
I was written by a sub procedure
VBScript Function Procedures
• A Function procedure:
1. is a series of statements, enclosed by the Function and End
Function statements
2. can perform actions and can return a value
3. can take arguments that are passed to it by a calling procedure
4. without arguments, must include an empty set of parentheses
()
5. returns a value by assigning a value to its name
Development of Internet Application
1501CT - Sara Almudauh
VBScript Function Procedures
• Function myfunction()
some statements
myfunction=some value
End Function
or
• Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
Development of Internet Application
1501CT - Sara Almudauh
Example
• <!DOCTYPE html>
<html>
<body>
Today's date: 2/22/2016
A Function procedure can return a result.
<%
Function myfunction()
myfunction=Date()
End Function
response.write("Today's date: ")
response.write(myfunction())
%>
<p>A Function procedure can return a
result.</p>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
Calling a Procedure
• <!DOCTYPE html>
<html>
<body>
14
<%
Function myfunction(a,b)
myfunction=a+b
End Function
response.write(myfunction(5,9))
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
Calling a Procedure
• When you call a procedure you can use the Call statement, like
this:
Call MyProc(argument)
Or, you can omit the Call statement, like this:
MyProc argument
Development of Internet Application
1501CT - Sara Almudauh
VBScript Conditional Statements
• In VBScript we have four conditional statements:
If 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
Development of Internet Application
1501CT - Sara Almudauh
VBScript Conditional Statements
• 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 response.write("Hello")
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
response.write("Hello")
i = i+1
End If
Development of Internet Application
1501CT - Sara Almudauh
VBScript Conditional Statements
• There is no ..Else.. in the example above 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:
<!DOCTYPE html>
<html>
<body>
Good morning!
<%
i=hour(time)
If i < 10 Then
response.write("Good morning!")
Else
response.write("Have a nice day!")
End If
%>
</body>
Development of Internet Application
1501CT - Sara Almudauh
VBScript Conditional Statements
• If...Then...ElseIf
• You can use the If...Then...ElseIf statement if you want to select one of many
blocks of code to execute:
<html>
<body>
<%
i=hour(time)
If i = 10 Then
response.write("Just started...!")
ElseIf i = 11 Then
response.write("Hungry!")
ElseIf i = 12 Then
response.write("Ah, lunch-time!")
ElseIf i = 16 Then
response.write("Time to go
home!")
Else
response.write("Unknown")
End If
%>
</body>
Good morning!
Development of Internet Application
1501CT - Sara Almudauh
Select Case
• You can also use the "Select Case" statement if you want to select
one of many blocks of code to execute:
d=weekday(Date)
Select Case d
Case 1
response.write("Sleepy Sunday")
Case 2
response.write("Monday again!")
Case 3
response.write("Just Tuesday!")
Case 4
response.write("Wednesday!")
Case 5
response.write("Thursday...")
Case 6
response.write("Finally Friday!")
Case Else
response.write("Super Saturday!!!!")
Development of Internet Application
1501CT - Sara Almudauh
End Select
VBScript Looping
• In VBScript we have four looping statements:
1. For...Next statement - runs code a specified number of times
2. For Each...Next statement - runs code for each item in a
collection or each element of an array
3. Do...Loop statement - loops while or until a condition is true
4. While...Wend statement - Do not use it - use the Do...Loop
statement instead
Development of Internet Application
1501CT - Sara Almudauh
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>
<%
For i = 0 To 5
response.write("The number is " & i & "<br />")
Next
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Do...Loop
• If you don't know how many repetitions you want, use a
Do...Loop statement.
• The Do...Loop statement repeats a block of code while a
condition is true, or until a condition becomes true.
• Repeat Code While a Condition is True
• You use the While keyword to check a condition in a Do...Loop
statement.
Do While i>10
some code
Loop
Development of Internet Application
1501CT - Sara Almudauh
Read more about Loops in VB
• http://www.w3schools.com/asp/vbscript_looping.asp
• http://www.tutorialspoint.com/vbscript/
• http://www.pickatutorial.com/tutorial/vbscript/first_vbscript_co
de.htm
Development of Internet Application
1501CT - Sara Almudauh
VBScript Examples
Development of Internet Application
1501CT - Sara Almudauh
Display Date and Time
• <!DOCTYPE html>
<html>
<body>
Today's date is 2/22/2016
The time is 2:30:17 PM
<%
response.write("Today's date is " & Date())
response.write("<br>")
response.write("The time is " & Time())
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
Display Days
• <!DOCTYPE html>
<html>
<body>
<p>VBScripts' function <b>WeekdayName</b> is used to get a weekday:</p>
<%
response.write("<p>")
response.write(WeekDayName(1))
response.write("<br>")
response.write(WeekDayName(2))
response.write("</p><p>")
response.write("Get the abbreviated name of a weekday:")
response.write("<br>")
response.write(WeekDayName(1,True))
response.write("<br>")
response.write(WeekDayName(2,True))
response.write("</p><p>")
response.write("Get the current weekday:")
response.write("<br>")
response.write(WeekdayName(weekday(Date)))
response.write("<br>")
response.write(WeekdayName(weekday(Date), True))
response.write("</p>")
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
VBScripts' function WeekdayName is
used to get a weekday:
Sunday
Monday
Get the abbreviated name of a weekday:
Sun
Mon
Get the current weekday:
Monday
Mon
Display the current month and day
• <!DOCTYPE html>
<html>
<body>
<%
response.write("Today's day is " &
WeekdayName(Weekday(Date)))
response.write("<br>")
response.write("The month is " &
MonthName(Month(Date)))
%>
</body>
</html>
Development of Internet Application
1501CT - Sara Almudauh
Today's day is Monday
The month is February
More Examples
• http://www.w3schools.com/asp/vbscript_examples.asp
Development of Internet Application
1501CT - Sara Almudauh
Questions !!
Development of Internet Application
1501CT - Sara Almudauh
© Copyright 2026 Paperzz