Tuesday, June 29, 2010

How to print the first digit of the numbers

When i input a number, my result omit the first digit and display the remaining numbers ex: if my input is 4321, it should print 4321, 321, 21, 1.

no=4321
While no>=1
msgbox no
noi=len(no)
div=divVal(noi-1)
no=no mod div
Wend

Function divVal(x)
val=1
For i=1 to x
val=val*10
Next
divVal=val
End Function

If you dont bother of type casting, you can also use:
no=cstr("4321")
for i=1 to len(no)
counter=0
msgbox mid(no,i,len(no)-counter)
counter=counter+1
next

Sunday, May 23, 2010

How to create class and constructor using VBScript

Class sampleClass
Private str

Private sub Class_Initialize()
msgbox("I am constructor")
End Sub

Private sub Class_Terminate()
msgbox("I am destructor")
End Sub

Public function setString(strVar)
str=strVar
End Function

Public function getString()
getString=str
End Function
End Class

Set strObj= New sampleClass
strObj.setString("Uday")
temp=strObj.getString
msgbox(temp)

How to execute a stored procedure using vbscript

conStr="driver=sql server;server=ServerName;database=test;uid=sa;pwd=sa" 'This is my connection string
Set cmdObj=createobject("adodb.command")
Set recObj=createobject("adodb.recordset")
With cmdObj
.activeconnection=conStr
.commandtype=4
.commandtext="SP_Insert_EMP" 'This is my SP name
.parameters.refresh
.parameters(1).value=22 'These are input parameters for SP
.parameters(2).value="xyz"
.execute 'The SP will be executed after this statement
End with

Tuesday, May 18, 2010

Script to reverse a number in vbscript

num=inputbox("Enter a number")
result=0
While num>0
digit=num mod 10
num=int(num/10)
result=result*10+digit
Wend
msgbox result

Find largest and smallest values in an array

Dim arr(6)
arr(0)=44
arr(1)=32
arr(2)=99
arr(3)=7
arr(4)=10
arr(5)=1
arr(6)=6
'Finding the largest
largest=arr(0)
For i=1 to ubound(arr)
If largest largest=arr(i)
End If
Next
msgbox largest

'Finding the smallest
smallest=arr(0)
For i=1 to ubound(arr)
If smallest>arr(i) Then
smallest=arr(i)
End If
Next
msgbox smallest

Thursday, May 13, 2010

What are DDL, DML, DCL and TCL in Database

Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:

* CREATE - to create objects in the database
* ALTER - alters the structure of the database
* DROP - delete objects from the database
* TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
* COMMENT - add comments to the data dictionary
* RENAME - rename an object

Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:

* SELECT - retrieve data from the a database
* INSERT - insert data into a table
* UPDATE - updates existing data within a table
* DELETE - deletes all records from a table, the space for the records remain
* MERGE - UPSERT operation (insert or update)
* CALL - call a PL/SQL or Java subprogram
* EXPLAIN PLAN - explain access path to data
* LOCK TABLE - control concurrency

Data Control Language (DCL) statements. Some examples:

* GRANT - gives user's access privileges to database
* REVOKE - withdraw access privileges given with the GRANT command

Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.

* COMMIT - save work done
* SAVEPOINT - identify a point in a transaction to which you can later roll back
* ROLLBACK - restore database to original since the last COMMIT
* SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Tuesday, May 11, 2010

How to reverse a string using vbscript

str="uday"
For i=len(str) to 1 step -1
strchar=mid(str,i,1)
resultstr=resultstr&strchar
Next
msgbox resultstr

or you can use the built-in function in vbscript
msgbox strreverse("uday")

Friday, May 7, 2010

How to check text displayed in a image

Use "GetTextLocation" method to Checks whether a specified text string is contained in a specified window area. If the text string is located, the location coordinates are also returned.

TextUtil.GetTextLocation(TextToFind, hWnd, Left, Top, Right, Bottom[, MatchWholeWords])

TextToFind : The text string you want to locate
hWnd : The handle to a run-time object's window(If hWnd is not 0, then the coordinate arguments apply to the specified window. If hWnd is 0, the coordinates apply to the screen.)
Left, Top, Right, Bottom : These arguments define the search area within the window or screen. Set all coordinates to -1 to search for the text string within the entire window or screen. The method returns the coordinates of the rectangle containing the first instance of the text into these variables if the text is found.

MatchWholeWordOnly : Optional. If True, the method searches for occurrences that are whole words only and not part of a larger word. If False, the method does not restrict the results to occurrences that are whole words only.
Default value = True

Script:

l = -1
t = -1
r = -1
b = -1

Succeeded = TextUtil.GetTextLocation("your search text",0,l,t,r,b)
msgbox Succeeded

Work with dictionary objects

Set dictObj=createobject("Scripting.Dictionary")
dictObj.Add "name","Uday"
dictObj.Add "tech","qa"
dictObj.Add "loc","hyd"
dictObj.Key("loc")="location"
msgbox dictObj.Item("location")
msgbox dictObj.Count

How do you create classes and objects using vbscript

Class math

Public function addition(a,b)
addition=a+b
End Function

Public function substraction(a,b)
substraction=a-b
End Function
End Class

Set mathObj=new math
msgbox mathObj.addition(2,5)
msgbox mathObj.substraction(9,3)
Set mathObj=nothing

Monday, May 3, 2010

Write a script to find out the number of links displayed in a web page

Set linkObj=description.Create
linkObj("micclass").value="link"

Set linkchildObjs=browser("xxx").Page("yyy").ChildObjects(linkObj)

childCount=linkchildObjs.count

For i=0 to childCount-1
msgbox linkchildObjs(i).getROProperty("innertext")
Next

If you want to click on the last link on that page:
browser("xxx").Page("yyy").link("index:="&childCount-1).click

Set linkObj=nothing


Following is the code to check all checkboxes:
Set cbObj=description.Create
cbObj("html tag").value="INPUT"
cbObj("type").value="checkbox"

set childObj=browser("xxx").Page("yyy").ChildObjects(cbObj)

For i=0 to childObj.count-1
childObi(1).set "ON"
Next

Monday, April 26, 2010

Print prime numbers

This logic finds all the prime numbers upto the number specified.

flag=1
primeNo=inputbox("Enter a number")
For i=2 to primeNo
flag=1
For j=2 to i/2
If i mod j=0 Then
flag=0
End If
Next

If flag=1 Then
msgbox i
End If
Next

Wednesday, April 14, 2010

Algorithm to check palindrome

Word="level"
lengthVar = Len(Word)
For i = 1 To lengthVar
v1 = v1 + Mid(Word, i, 1)
Next
For i = lengthVar To 1 Step -1
v2 = v2 + Mid(Word, i, 1)
Next
If v1 = v2 Then
MsgBox "given word is palindrome"
Else
MsgBox "given word is not palindrome "
End If

Monday, April 12, 2010

How to access Database using QTP

Method 1:
Option explicit
Dim connectionObj,recordsetObj,commandObj
Dim connectionStr,queryStr

Set connectionObj=createobject("ADODB.connection")
Set commandObj=createobject("ADODB.command")
Set recordsetObj=createobject("ADODB.recordset")
connectionStr="driver=sql server;server=xyz;database=test;uid=sa;pwd=sa"
queryStr="insert into studentdetails (stdno,stdname,age,groupname,sex,dateofbirth) values (6,'xyz',22,'ijk','M','08/18/80')"


connectionObj.open connectionStr
set commandObj.activeconnection=connectionObj
commandObj.commandtext=queryStr
Set recordsetObj=commandObj.execute
connectionObj.close
'commandObj.close
recordsetObj.close


If you have set of records in result set and if you want to travese through each of these then use below code:

while not recordsetObj.EOF
noRecords=recordsetObj.fields.count 'This finds the no. of columns in result set
var1=recordsetObj("fieldname1").value 'This retrieve the columnname values
var2=recordsetObj("fieldname2").value
recordsetObj.movenext
wend



Method 2
Option explicit
Dim connectionObj,recordsetObj,commandObj
Dim connectionStr,queryStr
Set commandObj=createobject("ADODB.command")
Set recordsetObj=createobject("ADODB.recordset")
connectionStr="driver=sql server;server=xyz;database=test;uid=sa;pwd=sa"
queryStr="insert into studentdetails (stdno,stdname,age,groupname,sex,dateofbirth) values (6,'xyz',22,'ijk','M','08/18/80')"
commandObj.activeconnection=connectionStr
commandObj.commandtext=queryStr
Set recordsetObj=commandObj.execute
'connectionObj.close
'commandObj.close
'recordsetObj.close

Write a query to find the Nth largest salary of an employee

Select * From Employee E1 Where
(N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

for ex: for 3rd largest salary of an employee, the query is:
Select * From Employee E1 Where
2 = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)