How to access Excel as a database using UFT
Whenever we access excel through UFT, we always use the createobject method which blocks the excel and starts excel.exe process. It can result in problems if the test script stops abruptly.
To resolve this issue you can access the excel as a database through UFT. Below code will help you with that.
Dim oConn, oRS, arrData, x,iStart
CONST adOpenStatic = 3
CONST adLockOptimistic = 3
CONST adCmdText = "&H0001"
Set oConn = CreateObject("ADODB.Connection")
Set objRecSet= CreateObject("ADODB.RecordSet")
'Open Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data Source=" & <ExcelFilePath> & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
sQuery = "Select * From [" <sheetName > "$]"
'Run query against WorkBook
objRecSet.Open sQuery, oConn, 3, 3, 1
'Move RecordSet to the target Row
For x = 2 to IterationIndex - 1
objRecSet.MoveNext
Next
'Use a For..Loop to iterate through Recordset
For x = 0 to objRecSet.Fields.Count - 1
msgbox objRecSet(x).Name
msgbox objRecSet(x).Value
Next
set oConn = Nothing
set objRecSet = Nothing
To resolve this issue you can access the excel as a database through UFT. Below code will help you with that.
Dim oConn, oRS, arrData, x,iStart
CONST adOpenStatic = 3
CONST adLockOptimistic = 3
CONST adCmdText = "&H0001"
Set oConn = CreateObject("ADODB.Connection")
Set objRecSet= CreateObject("ADODB.RecordSet")
'Open Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data Source=" & <ExcelFilePath> & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
sQuery = "Select * From [" <sheetName > "$]"
'Run query against WorkBook
objRecSet.Open sQuery, oConn, 3, 3, 1
'Move RecordSet to the target Row
For x = 2 to IterationIndex - 1
objRecSet.MoveNext
Next
'Use a For..Loop to iterate through Recordset
For x = 0 to objRecSet.Fields.Count - 1
msgbox objRecSet(x).Name
msgbox objRecSet(x).Value
Next
set oConn = Nothing
set objRecSet = Nothing
Comments
Post a Comment