Working with ADODB connections using QTP

How to open a password-protected database.

Example:

Dim FlightNumber

Dim dbexample

‘ Create the conection object.

Set dbexample = CreateObject(“ADODB.Connection”)

‘Set the connection string and open the connection

dbexample.ConnectionString = “DBQ=D:\Program Files\Mercury Interactive\WinRunner\samples\flight\app\flight32.mdb;DefaultDir=D:\Program Files\Mercury Interactive\WinRunner\samples\flight\app;Driver={Microsoft Access Driver (*.mdb)};DriverId=281;FIL=MS Access;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UserCommitSync=Yes;”

dbexample.Open

‘or use this method if a DSN entry was created.

‘dbexample.Open(“DSN=Flight32”)

flightnumber = 6195

‘ Get the recordset returned from a select query.

Set recordset = dbexample.Execute(“SELECT * from Orders WHERE Flight_Number = ” & flightnumber)

‘ Display the results of the query.

msgbox recordset.GetString

‘ Close the database connection.

dbexample.Close

Set dbexample = Nothing

If your query returns several columns, you can use the Fields method to retrieve data from specified columns in the returned record set.

 Example:

‘ Connect to the Flight32 database

Set dbexample = CreateObject(“ADODB.Connection”)

dbexample.Open(“DSN=Flight32”)

‘ Perform a query

Set recordset = dbexample.Execute(“SELECT * from Orders”)

‘ Get the values from the Customer_Name column

while (NOT recordset.EOF)

MsgBox recordset.Fields(“Customer_Name”)

‘ Move to the next value in the record set

recordset.MoveNext

wend

‘ Close the database connection.

dbexample.Close

Set dbexample = Nothing

Message Box closes automatically using windows shell

Dim msg
msg = “Message Box automatically close in 5 second”
Set oShell = CreateObject(“WScript.Shell”)
oShell.Popup msg,10, “QTP Challenges”
In above statement, there are three arguments –
msg – the content/string of the message box
10 – the time in seconds
“QTP  Challenges” – the title of the message box