Professional Documents
Culture Documents
Page
V B SAMPLES
Dim oNumber
oNumber=4
If oNumber mod 2 <>0 Then
Print "The Number "& oNumber &" is an Odd Number"
else
Print "The Number "& oNumber &" is not an Odd Number"
End If
3
Dim RangeStart
Dim RangeEnd
Dim iCounter
RangeStart=10
RangeEnd=20
For iCounter=RangeStart to RangeEnd
If iCounter mod 2 <>0 Then
Print oNumber
End If
Next
4
Dim oNumber
Dim iCounter
oNumber=6
2
Page
Dim fValue
fValue=1
For iCounter=oNumber to 1 step-1
fValue=fValue*iCounter
Next
print fValue
5
Dim oNumber
Dim iCounter
oNumber=10
For iCounter=1 to oNumber/2
If oNumber mod iCounter=0 Then
print iCounter
End If
Next
print oNumber
6
Dim RangeStart
Dim RangeEnd
Dim iCounter
RangeStart=1
RangeEnd=30
For iCounter=RangeStart to RangeEnd
For iCount=2 to round(iCounter/2)
Exit for
3
Page
End If
Next
If iCount=round(iCounter/2)+1 or iCounter=1 Then
print iCounter
End If
Next
7
Dim oNum1
Dim oNum2
oNum1=1055
oNum2=155
oNum1=oNum1-oNum2
oNum2=oNum1+oNum2
oNum1=oNum2-oNum1
print oNum1
print oNum2
8 Write a program to Perform specified Arithmetic Operation on
two given numbers
Dim oNum1
Dim oNum2
Dim oValue
oNum1=10
oNum2=20
4
Page
OperationtoPerform="div"
oValue=oNum1*oNum2
Case "div"
oValue=oNum1/ oNum2
End Select
print oValue
9
Dim oStr
Dim oLength
oStr="sudhakar"
oLength=len(oStr)
print oLength
10
Dim oStr
Dim oLength
Dim oChar
Dim iCounter
oStr="sudhakar"
oLength=len(oStr)
5
Page
oChar=oChar&mid(oStr,iCounter,1)
Next
print oChar
11
Dim oStr
Dim oLength
Dim oChar
Dim iCounter
oStr="su1h2kar"
oLength=len(oStr)
oAlphacounter=0
For iCounter=1 to oLength
If not isnumeric (mid(oStr,iCounter,1)) then
oAlphacounter=oAlphacounter+1
End if
Next
print oAlphacounter
12
Dim oStr
Dim oArray
Dim ochr
oStr="sudhakar"
ochr="a"
oArray=split(oStr,ochr)
13
6
Page
print ubound(oArray)
Dim oStr
Dim fStr
oStr="Quick Test Professional"
fStr=replace(oStr," ",vbtab)
print fStr
14
Dim ochr
Dim aVal
ochr="A"
aVal=asc(ochr)
print aVal
15 Write a program to return character corresponding to the given
ASCII value
Dim ochr
Dim aVal
aVal=65
oChr=chr(aVal)
print oChr
16
Dim oStr
Dim uStr
oStr="QuickTest Professional"
uStr=ucase(oStr)
7
Page
print uStr
17
Dim oStr
Dim lStr
oStr="QuickTest Professional"
lStr=lcase(oStr)
print lStr
18 Write a program to Replace a word in a string with another
word
Dim oStr
Dim oWord1
Dim oWord2
Dim fStr
oStr="Mercury Quick Test Professional"
oWord1="Mercury"
oWord2="HP"
fStr=replace(oStr,oWord1,oWord2)
print fStr
19
Dim oStr
oStr="bob"
fStr=StrReverse(oStr)
If oStr=fStr Then
Print "The Given String "&oStr&" is a Palindrome"
8
Page
else
Dim oStr1
Dim ostr2
oStr1="qtp"
oStr2="qtp"
If oStr1=oStr2 Then
Print "The Given Strings are Equal"
else
Print "The Given Strings are not Equal"
End If
21
Dim oArray
Dim oCounter
oArray=array(1,2,3,4,"qtp","Testing")
For oCounter=lbound(oArray) to ubound(oArray)
print oArray(oCounter)
Next
22
Dim oArray
Dim oCounter1
Dim oCounter2
Dim tmp
9
Page
oArray=array(8,3,4,2,7,1,6,9,5,0)
Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
10
oArray2(1,1)=0
Page
tArray(0,0)=oArray1(0,0)+ oArray2(0,0)
tArray(0,1)=oArray1(0,1)+oArray2(0,1)
tArray(1,0)=oArray1(1,0)+oArray2(1,0)
tArray(1,1)=oArray1(1,1)+oArray2(1,1)
24
Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
oArray2(1,1)=0
tArray(0,0)=oArray1(0,0)* oArray2(0,0)+ oArray1(0,1)* oArray2(1,0)
tArray(0,1)=oArray1(0,0)* oArray2(0,1)+ oArray1(0,1)* oArray2(1,1)
tArray(1,0)=oArray1(1,0)* oArray2(0,0)+ oArray1(1,1)* oArray2(1,0)
tArray(1,1)=oArray1(1,0)* oArray2(0,1)+ oArray1(1,1)* oArray2(1,1)
25
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
print StrArray(iCounter)
11
Page
StrArray=split(oStr)
Next
26
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr,"i")
For iCounter=0 to ubound(StrArray)
print StrArray(iCounter)
Next
27
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr," ")
print "Theere are "&ubound(StrArray)+1&" words in the string"
28
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr," ")
For iCounter=0 to ubound(StrArray)
print strreverse(StrArray(iCounter))
29
12
Next
13
Page
End If
End Function
30
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr," ")
print join(StrArray," ")
31
Dim oStr
oStr="
QTP
"
print trim(oStr)
32 Write a program to insert 100values and to delete 50 values
from an array
Dim oArray()
Dim iCounter
ReDim oArray(100)
For iCounter=0 to ubound(oArray)
oArray(iCounter)=iCounter
'Print total 100 Values
print(oArray(iCounter))
Next
print "******************************"
ReDim preserve oArray(50)
Page
14
print "******************************"
Option explicit
Dim x
x=10
'Here we get an error because i have not declared y,z
y=20
z=x+y
print z
34
Dim oArray()
if isarray(oArray) then
print "the given variable is an array"
else
print "the given variable is not an array"
Dim objWMIService
Page
36
15
End if
Dim colTimeZone
Dim objTimeZone
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery("Select * from
Win32_TimeZone")
For Each objTimeZone in colTimeZone
print "Offset: "& objTimeZone.Bias
Next
37 Retrieving Time Zone Information for a Computer
Dim objWMIService
Dim colTimeZone
Dim objTimeZone
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery("Select * from
Win32_TimeZone")
For Each objItem in colTimeZone
print "Bias: " & objItem.Bias
print "Caption: " & objItem.Caption
print "Daylight Bias: " & objItem.DaylightBias
print "Daylight Day: " & objItem.DaylightDay
print "Daylight Day Of Week: " & objItem.DaylightDayOfWeek
16
Dim StrDate
Dim actualDate
Dim StrTime
Dim actualTime
17
Print day(date)
43
Dim oCurrentMonth
Dim ocurrentYear
Dim oDaysinMonths
oCurrentMonth = Month(date)
ocurrentYear = Year(date)
18
If oDaysinMonths=31 Then
Page
'1st Method
'The rules for leap year:
'1. Leap Year is divisible by 4
2nd Method
19
Page
oYear=1996
Dim oNum
Dim DecimaPlacestobeFormat
oNum = 3.14159
DecimaPlacestobeFormat=2
print Round(oNum , DecimaPlacestobeFormat)
47
Page
20
'Both Int and Fix remove the fractional part of number and return the
resulting integer value.
'The difference between Int and Fix is that if number is negative, Int returns
the first negative integer less than or equal to number,
'whereas Fix returns the first negative integer greater than or equal to
number.
'For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
print Int(99.8)
print Fix(99.2)
Dim oVar
Dim oDatatypes
oVar="QTP"
oVartype=Typename(oVar)
print oVartype
50
Dim oNum
oNum=3.123
oDecNum=oNum- int(oNum)
print oDecNum