VBScript Basics
VBScript Basics
VBScript
10/22/08 2
Pros. and Cons. of Runtime Compilation
Advantages of Runtime Compilation:
Can be embedded with other types of code.
Transparent code.
10/22/08 3
Advantages of Using VBScript
10/22/08 4
What Can You Do with VBScript
10/22/08 5
Data Types
10/22/08 6
Why Data Types are Important
10/22/08 8
Arrays as Complex Data Types
• What is an Array?
• Arrays Have Dimensions
• Array Bounds and Declaring Arrays
• Accessing Arrays with Subscripts
• Looping through Arrays
• Erasing Arrays
• Using VarType() with Arrays
10/22/08 9
What is a Variable?
10/22/08 10
Option Explicit
10/22/08 11
Procedures and Functions
10/22/08 12
Procedures and Functions
Procedure Syntax
[Public| Private] Sub Name([Arg1], [Arg2])
[code inside the procedure]
End Sub
10/22/08 13
Procedures and Functions
• Public is by default
• Rules for naming a procedure is same as that of declaring
a variable.
Function Syntax
[Public | Private] Function Name ([Arg1], [Arg2])
[Code of the function ]
End Function
10/22/08 14
Calling Procedures and Functions
Procedure Calling :
Legal
Greet User “Bill”
Call Greet User (“Bill”)
Not legal
Greet User (“Bill”)
Call Greet User “Bill”
10/22/08 15
Calling Procedures and Functions
Function Calling :
Returning a value
Legal
LngSum = AddNumbers(10,20)
Illegal
LngSum = AddNumbers 10,20
Not Returning a value
10/22/08 16
Calling Procedures and Functions
Legal
Call AddNumbers(10,20)
Illegal
LngSum = Call AddNumbers(10,20)
Legal
AddNumbers 10,20
10/22/08 17
Optional Arguments
10/22/08 18
Exiting a Procedure or Function
10/22/08 19
Inbuilt features
Array Function
Returns a Variant containing an array.
Eg. A = Array(10,20,30) ‘returns an array
10/22/08 20
Built in Functions
10/22/08 21
Built in Functions
Join Function
Returns a string created by joining a number of substrings contained in an
array.
Join(list[, delimiter])
The default delimiter is space.
Eg. Join(myArray, “,”) ‘Returns a concantenated string of all the elements in
the array delimited by a comma .
Left Function
Returns a specified number of characters from the left side of a string.
Left(string, length)
Eg. MyString = Left(“abcd”, 3) returns abc
Right Function
Returns a specified number of characters from the right side of a string.
Right(string, length)
Eg. MyString = Left(“abcd”, 3) returns abc
10/22/08 22
Built in Functions
10/22/08 23
Built in Functions Examples
1.InStr--Returns the position of the first occurrence of one string within another. The search begins at
the first character of the string.
Eg: Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4.
Returns 6.
2.Mid-Returns a specified number of characters from a string
Dim MyString
MyString = "The dog jumps" ' Initialize string.
Mid(MyString, 5, 3) = "fox" ' MyString = "The fox jumps".
Mid(MyString, 5) = "cow" ' MyString = "The cow jumps".
3.StrComp-Compares two strings and returns a value that represents the result of the comparison
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.if both equal
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1. MyStr2> MyStr1
MyComp = StrComp(MyStr2, MyStr1) ' Returns 1. MyStr1> MyStr2
4.Len-Returns the number of characters in a string
10/22/08 24
5. LTrim-Removes spaces on the left side of a string
VBScript Keywords
10/22/08 26
Variable Declarations and Scope
10/22/08 27
Design Strategies for Scripts and Procedures
Here are some general principles to aid you in your script designs:
• Simple script files that perform one specific job with a limited
amount of code can be written as a single block of code without
any procedures or functions.
• As script files become more complex, look for ways to break the
logic down into subparts using, procedures, functions, and / or
classes.
• As you break the logic into subparts, keep the coordinating code
at the top of the script file.
10/22/08 29
Literals and Named Constants
What is a Literal?
A literal is any piece of static data that appears in your
code that is not stored in a variable or named
constant. Literals can be strings of text, numbers,
dates, or Boolean values. For examples, the word
“Hello” in the Following code is a literal.
Literals:
• String literal is enclosed in quotation marks (" ").Eg A
= “123”
• Date literals and time literals are represented by
enclosing them in number signs (#) Eg. Const
CutoffDate = #6-1-97#
Example: Dim strMessage
strMessage = “Hello”
MsgBox strMessage
10/22/08 30
Literals and Named Constants
10/22/08 31
Using Named constants in place of Literals
10/22/08 32
Built-In VBScript Constants
10/22/08 33
Conditional Statements
If...Then...Else
To run only one statement for a True condition, use the single-line syntax.
Eg. If myDate < Now Then myDate = Now
To run more than one line of code, you must use the multiple-line (or block)
syntax. This syntax includes the End If statement
Eg. If value = 0 Then
AlertLabel.Font.Italic = True
End If
Deciding Between Several Alternatives
Using ElseIf and Else clauses, you can control program flow based on
different possibilities.
Eg. If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
Else
Msgbox "Value out of range!“
End If 10/22/08 34
Select Case
A Select Case structure works with a single test expression that is evaluated
once, at the top of the structure. The result of the expression is then compared
with the values for each Case in the structure. If there is a match, the block of
statements associated with that Case is executed.
Do Loops
The statements are repeated either while a condition is True or until a
condition becomes True.
Eg. Do While myNum > 10
myNum = myNum – 1
counter = counter + 1
Loop
Eg. Do Until myNum = 10
myNum = myNum – 1
counter = counter + 1
Loop
While...Wend
Eg. while myNum <> 10
myNum = myNum – 1
counter = counter + 1
wend
*Because of the lack of flexibility in While...Wend, it is recommended that
you use Do...Loop instead.
10/22/08 36
For...Next
For loops, use a counter variable whose value increases or decreases with each repetition of the
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
Using the Step keyword, you can increase or decrease the counter variable by the value you
specify.
Eg . For j = 2 To 10 Step 2
total = total + j
Next
Step can be any integer. Default step is 1.
For Each…Next
This statement is used to iterate over a collection.
Dim names(2)
names(0) = "Tove"
names(1) = "Jani"
names(2) = "Hege"
For Each x In names
MsgBox x
Next
End Sub
Result:Displays “Tove” first time,next “Jani”,next time “Hege”
10/22/08 37
Exit Statement
10/22/08 38