Basic of VB Script
Basic of VB Script
S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for Women,Karur.
CONTENTS OF VB SCRIPT
• INTRODUCTION
• DATA TYPE
• VARIABLE
• OPERATORS
• CONTROL STRUCTURE
• BUILT IN FUNCTION
• ADVANTAGES OF VB SCRIPT
• DISADVANTAGES OF VB SCRIPT
INTRODUCTION
• <html>
• <body>
• <script type="text/vbscript">
document.write("Hello World")
• </script>
• </body>
• </html>
• VBScript only runs on Internet Explorer browsers.
• Output: Hello World
vbscript <script> tag
<script type="text/vbscript">
document.write("No semicolons")
document.write(" were injured in the making")
document.write(" of this tutorial!")
</script>
• Output:
• No semicolons were injured in the making of
this tutorial!
vbscript multiple line syntax
<script type="text/vbscript">
document.write("This is a very long write " &_
"statement that will need to be placed onto
three " &_ "lines to be readable!")
</script>
• Output:
• This is a very long write statement that will
need to be placed onto three lines to be
readable!
vbscript syntax review
Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values;
1.401298E-45 to 3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324
for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999.
String Contains a variable-length string that can be up to approximately 2 billion characters in length.
• <script type="text/vbscript">
• Option Explicit
• Dim firstName
• Dim age
• firstName = "Borat“
• age = 25
• </script>
Display your Variables
• <script type="text/vbscript">
• Option Explicit
• Dim firstName
• Dim age
• firstName = "Borat"
• age = 25
• document.write("Firstname: " & firstName & "</br />")
• document.write("Age: " & age)
• </script>
• Output:
Firstname: Borat
Age: 25
Lifetime of Variables
• A For Each...Next loop repeats a block of code for each item in a collection, or for each element of
an array.example:
• <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>
• Output:
• Volvo
Saab
BMW
The do while loop
• The Do … Loop statement is very useful (like, for …
next and Select … case) to execute a block of code
more than once. However, where it differs from the
other two is that the Do … Loop statement executes
the code as long as (while) a condition is true or until a
condition becomes true.
• Typical uses of a Do … Loop are in cases where you
may not know how many times you need to execute
the code section because it could vary from situation
to situation, like when reading in a file or taking in
keyboard input.
Example:
• <script type="text/javascript">
Dim num num = 0
• do while num < 30
• num = num + 5
• document.write(num & "<br />")
• loop
• </script>
• Output:
• 5
• 10
• 15
• 20
• 25
• 30
•
In the above example, a variable named num is given the value 0. The condition in the while loop is
that while num is less than 30, 5 should be added to num. Once the value of num is greater than
30, the loop will stop executing. The loop prints the current value of numfollowed by a line break
through each iteration.
BUILT IN FUNCTION
DateAdd Returns a date to which a specified time interval has been added
DateSerial Returns the date for a specified year, month, and day
Day Returns a number that represents the day of the month (between 1 and 31, inclusive)
Hour Returns a number that represents the hour of the day (between 0 and 23, inclusive)
IsDate Returns a Boolean value that indicates if the evaluated expression can be converted to a date
Minute Returns a number that represents the minute of the hour (between 0 and 59, inclusive)
Month Returns a number that represents the month of the year (between 1 and 12, inclusive)
Date/Time Functions
MonthName Returns the name of a specified month
CDate Converts a valid date and time expression to the variant of subtype Date
Function Description
FormatCurrency Returns an expression formatted as a currency
value
FormatDateTime Returns an expression formatted as a date or time
FormatNumber Returns an expression formatted as a number
FormatPercent Returns an expression formatted as a percentage
Math Functions
Function Description
Filter Returns a zero-based array that contains a subset of a string array based on a filter
criteria
IsArray Returns a Boolean value that indicates whether a specified variable is an array
LBound Returns the smallest subscript for the indicated dimension of an array
UBound Returns the largest subscript for the indicated dimension of an array
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
Trim Removes spaces on both the left and the right side of 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
StrComp Compares two strings and returns a value that represents the result of the comparison
MsgBox Displays a message box, waits for the user to click a button, and returns a value
that indicates which button the user clicked
InputBox Displays a dialog box, where the user can write some input and/or click on a
button, and returns the contents
IsEmpty Returns a Boolean value that indicates whether a specified variable has been
initialized or not
IsNull Returns a Boolean value that indicates whether a specified expression contains no
valid data (Null)