There are two types of procedures in VBScript: sub procedures and function procedures. Sub procedures do not return a value and are used to perform actions, while function procedures return a value. VBScript also includes conditional statements like If/Then/Else for executing code based on conditions as well as looping statements like For/Next for repeating code a specified number of times. It also contains many built-in functions for manipulating strings, dates, and more.
1. VBScript Procedures
In VBScript, there are two types of procedures:
Sub procedure
Function procedure
A Sub procedure:
is a series of statements, enclosed by the Sub and End Sub statements
does not return a value
can take arguments
without arguments, it must include an empty set of parentheses ()
eg.
Sub displayName()
msgbox("QualityPoint Technologies")
End Sub
or
Sub addvalues(value1,value2)
msgbox(value1+value2)
End Sub
When calling a Sub procedure you can use the Call statement, like this:
Call MyProc(argument)
Or, you can omit the Call statement, like this:
MyProc argument
A Function procedure:
is a series of statements, enclosed by the Function and End Function statements
can return a value
can take arguments
without arguments, must include an empty set of parentheses ()
returns a value by assigning a value to its name
2. Find below a Sample function.
Function addvalues(value1,value2)
addvalues=value1+value2
End Function
The above function will take take two arguments and will add those two values and then it will
return the sum value. Note here the sum value is retured by assigning it to the function name.
The above function can be called as below.
msgbox "Sum value is " & addvalues(1,2)
Conditional Statements
In VBScript we have four conditional statements:
if statement - executes a set of code when a condition is true
(e.g) if i=10 then
msgbox "I am 10"
End if
if...then...else statement - select one of two sets of lines to execute
(e.g) if i=10 then msgbox "I am 10"
else msgbox "other than 10"
end if
if...then...elseif statement - select one of many sets of lines to execute
(e.g) if i=10 then msgbox "I am 10"
elseif i=11 then msgbox "I am 11"
else msgbox "unknown"
end if
select case statement - select one of many sets of lines to execute
select case value
case 1
msgbox "1"
case 2
msgbox "2"
case 3
msgbox "3"
case else
msgbox "other than 1,2 and 3"
end select
3. Looping Statements
Use the For...Next statement to run a block of code a specified number of times.
e.g
for i = 0 to 5
msgbox("The number is " & i )
next
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.
Built-in Functions
VBscript is having many useful built-in functions.
You can refer this page for the complete list
inStr, isNull, LCase, Left, Len, Mid, Now, Replace, Split, UBound, CStr, CreateObject, Date and
DatePart are functions that are most frequently used in QTP script development.