QTP Script for connecting to database

This post will give u steps(script)to connect to database using MS Access.

Option Explicit
Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.provider="microsoft.jet.oledb.4.0"
con.open"d:\testdata.mdb"
rs.open"select*from emp",con
Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

Database we are using here is MS Access.before running this script create a table in MS Acess.

In the above script i used table called "emp" and column names as "v1" and "v2".

"d:\testdata.mdb" is path of the table which we created.

Main use of this application is to use testdata of table(which is in database) in the application.

Qtp script for connecting to sql server and oracle

7 comments:

manasa said...

Try this.... its working

Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.provider="microsoft.jet.oledb.4.0"
con.open"C:\Documents and Settings\C5107978\My Documents\db1.mdb"
rs.open"select*from table1",con
Do while not rs.eof
Reporter.ReportEvent micPass, "access opened"," emp"
Dim val1,val2
set val1= rs.fields("emp")
set val2= rs.fields("con")
Reporter.ReportEvent micPass,val1," emp"
Reporter.ReportEvent micPass,val2," emp"

rs.movenext
Loop

F n K said...

hi manasa,

thnx for ur comment but u need to change ur script to get correct output.

in line rs.open"select*from table1"
u have to specify name of table.

n other mistake is while setting values of val1 and val2 u need to specify column names as
set val1=rs.fields("column1name")
set val2=rs.fields("column2name")
if u correct this remaining code is perfect check ur own code n check this changes u vl know difference.

Anonymous said...

HI..

Can u also provide script for connecting to database that is sqlserver..

I am trying this since many days but not able to succeed.. i would be very thankful if u provide me the script for connecting sql server

F n K said...

Hi,

check this link for ur query

http://funandknowledge.blogspot.com/2008/03/qt.html

Shiri said...

Hi guys,
For the above script, i am getting error in path. Anybody can tell me, other then mdb, we can use any other extension.

Shiri said...

The above script i tried but i am getting an error for the path. Anybody can tell me other then mdb, we can us any other extension.

Sreekanth's Projects said...

Hey i am getting following error, can anybody help:

"Data source name not found and no default driver specified"

Related Posts Plugin for WordPress, Blogger...