有什么方法可以将记录集值放入字典变量中?我已经写了相同的代码,但是没有用

问题描述 投票:-2回答:1

将记录集值分配给字典变量时,我遇到了问题。我已经编写了代码,并且值一直显示到循环。但是从循环中退出后,当我尝试使用键获取字典的值时,其显示为空白。即使我正在按预期获得字典变量的计数。请找到我尝试过的代码的附件图像。

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "PROVIDER=Microsoft.ACE.OLEDB.12.0;" & "Data Source=Path.xlsx;" & "Extended Properties=""Excel 8.0;HDR=Yes;"";"
Set objRecordSet = CreateObject("ADODB.Recordset")
Dim sql_text
sql_text = "Select Keyword, Value from [TestData$] where TestCaseName = 'TestcaseName1'"
objRecordset.Open sql_text, objConnection, 1
objRecordset.MoveFirst
MsgBox objRecordset.RecordCount
Dim Universal
Set Universal = CreateObject("Scripting.Dictionary")
For i = 0 To objRecordset.RecordCount-1 Step 1
    'MsgBox objRecordset.Fields("Keyword") 'This shows correct values
    'MsgBox objRecordset.Fields("Value")   'This shows correct values
    Universal.Add objRecordset.Fields("Keyword").Value, objRecordset.Fields("Value")
    'MsgBox Universal.Item(objRecordset.Fields("Keyword")) 'This shows correct values
    'MsgBox Universal.Item("TestData_TestCase1_Data"&i+1)  'This shows correct values
    objRecordset.MoveNext
Next
objRecordset.MoveFirst
MsgBox Universal.Count   'This shows correct values
MsgBox Universal.Item("TestData_TestCase1_Data2") 'Here I am facing issue to access the value.

objRecordset.Close
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
vbscript qtp hp-uft
1个回答
-1
投票

我找到了答案,请找到下面的代码。

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "PROVIDER=Microsoft.ACE.OLEDB.12.0;" & "Data Source= 
C:\UFT_Automation\Temporary\TestDatasheet.xlsx;" & "Extended Properties=""Excel 
8.0;HDR=Yes;"";"
Set objRecordSet = CreateObject("ADODB.Recordset")
Dim sql_text 
sql_text = "Select Keyword, Value from [TestData$] where TestCaseName = 
'TestcaseName2'"
objRecordset.Open sql_text , objConnection,1
objRecordset.Movefirst
Dim Universal
Set Universal = CreateObject("Scripting.Dictionary")
for i = 0 to objRecordset.recordcount-1 step 1
Universal.Add objRecordset.fields("Keyword").Value, 
objRecordset.fields("Value").Value
objRecordset.MoveNext
Next
objRecordset.Movefirst
msgbox Universal.Item("TestData_TestCase2_Data10")
objRecordset.Close
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.