VBScript notes
VBScript notes
Features of VBScript:
1. Easy to learn and use
2. Not case sensitive
3. Built in formatting: (built in features for formatting date and
numbers)
4. ASP Default language: (if we don’t specify language in ASP, it is
considering VBScript as default language)
5. Lightweight: (program of VBScript is very lightweight and can be
write using notepad)
6. Automation support: (it provides automation support we can
automate our tasks)
7. Conditional statements: (it supports if…Then)
8. Loops and repetition: (for next, do while, Do until)
9. Error Handling Support: (Error handling features we have error
object to handle i.e. Err)
10. ActiveX scripting: (means using script to control another windows
program automatically)
Data Types:
VBScript has only one data type called Variant.
A variant can contain either numeric or string information.
Variant behaves as a number when you use it in a numeric context
and as a string when you use it in a string context.
By default, data type of a variable is variant.
The different categories of information that can be contained in a
variant are called subtypes.
Name Size Description
Byte 1 byte Only positive values
Integer 2 bytes Whole numbers (–
32,768 to 32,767)
Long 4 bytes Larger whole numbers
Single 4 bytes Small decimal numbers
(.6)
Double 8 bytes Large/precise decimal
numbers (.14)
Currenc 8 bytes Monetary values, fixed
y decimal (.4)
String String Text values
length+10 bytes
Date 8 bytes Stores both date and
time
Boolean 2 bytes True or False
Object 4 bytes It is used to hold object
Empty 0 bytes Uninitialized variable
Variable:
It is a name given to storage location.
Value of a variable can be varied.
Variable has two associated things with it: a name and its datatype
It is used to store value.
All variable are treated as type variant by default.
Declaring Variable:
Variable are declared using “Dim” keyword.
In VBScript, you cannot specify a variable’s data type directly. All
variables are automatically of type Variant, which is a flexible data type
that can hold any kind of value — string, number, date, object, etc.
Ex: Dim name
Dim rollno , name
Constants:
Constant is a named given to memory location.
It is used to hold values that cannot be changed during the script
execution.
Syntax: const constant_name=value
MsgBox:
A message box is a function used to displays a pop-up message box to
the user.
It is used to show information, warnings, errors, or to ask the user for a
response
Syntax: MsgBox(prompt, [buttons], [title])
Constant Value Description
vbOKOnly 0 Display only the OK button
vbOKCancel 1 Display OK and Cancel buttons
vbAbortRetryIgnor 2 Display Abort, Retry, and Ignore
e buttons
vbYesNoCancel 3 Display Yes, No, and Cancel buttons
vbYesNo 4 Display Yes and No buttons
vbRetryCancel 5 Display Retry and Cancel buttons
InputBox:
InputBox is used to take input value from the user.
Syntax: InputBox(prompt,[title],[default value],[x pos,y pos]).
Conditional Statements:
It is used to execute part of code based on some condition.
If – then
If – Then – else
If – then- elseif-then-else
Select case
Syntax:
If -then
If (condition) then
Statements
End if
If – then – else
If (condition) then
Statements
Else
Statements
End if
Select case
Select case expression
Case value1
Statements
Case value2
Statemenets
Case value3
Statements
Case else
Statements
End select
Looping structure:
If we want to execute certain part of code repeatedly we can use loops
There are two types of looping structure:
1. Condition controlled
2. Counter controlled
DO While_loop
Do while condition
Statements
Loop
Do until_loop
Do until condition
Statements
Loop
While_wend
While condition
Statements
wend
For_Next
For counter=start value to end value [step value]
Statements
Next counter
Array
An array is a collection of values represented by a single variable name
each value in an array is accessed by its index i.e position.
Dim arrayname(size)
Or
Array_name= Array(value1, value2, value3….)
Multidimensional array
Syntax
Dim array_name(dimension1, dimension2)
Array functions
LBound(arrayname,[dimension])
Returns the Lowest index of the specified array.
UBound(arrayname,[dimension])
Returns the highest index of the specified array.
Split(string,[delimeter],[limit],[compare])
The Split function in VBScript is used to split a string into an array based
on a specified delimiter. It returns an array where each element is a
portion of the original string.
expression: The string you want to split.
delimiter (Optional): The character or substring used to divide the
string. If not provided, the default is a space (" ").
limit (Optional): The maximum number of substrings to return. If
omitted, all substrings are returned.
compare (Optional): A numeric value that defines how strings are
compared. 0 for case-sensitive comparison and 1 for case-insensitive.
Join Function
The Join function in VBScript is used to combine an array of strings into
a single string, with a specified delimiter between each element.
Syntax: Join(array, [delimiter])
array: The array of strings you want to join.
delimiter (Optional): The delimiter you want to insert between
each element of the array. The default delimiter is an empty string
("").
Filter Function
The Filter function in VBScript is used to search for a specific substring
within an array and return a new array containing only the elements
that match the search criteria.
IsArray(variable)
It is used to check a variable is array or not if the specified variable is
array it will return true otherwise it will give false.