QTP中数据字典的脚本出错,求大神看
本帖最后由 yinjiajia0626 于 2014-7-26 17:52 编辑'调用D盘的1.txt资源,在setting中
Class oDataDic
Private oDic 'DIctionary对象
Public oWorkBookPath 'xls文件对象
Public oSheetName
Public oRowNo 'Rows have number
Public Default Function Load(oWorkBookPath,oSheetName,oRowNo)
With Me
.oWorkBookPath=oWorkBookPath
.oSheetName=oSheetName
.oRowNo=oRowNo
End With
BuildContext
Set Load=oDic
End Function
Private Function BuildContext
Dim oConn,oRS,arrData,x
Set oConn=CreateObject("adodb.Connection")
Set oRS=CreateObject("adodb.RecordSet")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;"&_
"Data Source="&Me.oWorkBookPath&";"&_
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
sQuery="Select * from ["&Me.oSheetName&"$]"
oRs.Open sQuery,oConn,3,3,1
For x=2 to oRowNo -1:oRS.MoveNext:
Next
oDict=CreateObject("Scripting.Dictionary")
For x=0 to oRS.Fields.Count-1
msgboxoRS.Fields.Item(x)
With oDict
.Add "" & oRS(x).Name,"" & oRS.Fields(x)
End With
Next
End Function
Private Property Let oDict(ByVal val)
Set oDict=Val
End Property
Private Property Get oDict()
Set oDict = oDic
End Property
End Class
Set mDataContext=New oDataDic
oWorkBookPath="D:\1.xls"
oSheetName="Sheet1"
oRowNo=3
Set mDict=mDataContext.Load(oWorkBookPath,oSheetName,oRowNo)
MsgBox mDict("Age")
MsgBox mDict("Sex")
MsgBox mDict("Username")
MsgBox mDict("Password")
Set mDict=Nothing
出错的截图:
附件为我用到的1.xls 回复 1# yinjiajia0626
28行,定义数组用要Set
Set oDict=CreateObject("Scripting.Dictionary")
我没装你写的这个数据库的provider,不确认,不过建议你在细节化错误在哪,如果注释掉数据库那部分,把定义语句放在with里面,是可以的 回复 2# ermine
谢谢啊,Set oDict=CreateObject("Scripting.Dictionary")这个试了之后没有报错了,但是MsgBox弹出的是空值;
我是新手,还不太会细化错误。。。
总之还是很谢谢你! '调用D盘的1.txt资源,在setting中
Class oDataDic
Private oDic 'DIctionary对象
Public oWorkBookPath 'xls文件对象
Public oSheetName
Public oRowNo 'Rows have number
Public Default Function Load(oWorkBookPath,oSheetName,oRowNo)
With Me
.oWorkBookPath=oWorkBookPath
.oSheetName=oSheetName
.oRowNo=oRowNo
End With
BuildContext
Set Load=oDic
End Function
Private Function BuildContext
Dim oConn,oRS,arrData,x
Set oConn=CreateObject("adodb.Connection")
Set oRS=CreateObject("adodb.RecordSet")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;"&_
"Data Source="&Me.oWorkBookPath&";"&_
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
sQuery="Select * from ["&Me.oSheetName&"$]"
oRs.Open sQuery,oConn,3,3,1
For x=2 to oRowNo -1:oRS.MoveNext:
Next
oDict=CreateObject("Scripting.Dictionary")
For x=0 to oRS.Fields.Count-1
msgboxoRS.Fields.Item(x)
With oDict
.Add "" & oRS.Fields(x).Name,"" & oRS.Fields(x) End With
Next
End Function
Private Property Let oDict(ByVal val)
Set oDict=Val
End Property
Private Property Get oDict()
Set oDict = oDic
End Property
End Class
Set mDataContext=New oDataDic
oWorkBookPath="D:\1.xls"
oSheetName="Sheet1"
oRowNo=3
Set mDict=mDataContext.Load(oWorkBookPath,oSheetName,oRowNo)
MsgBox mDict("Age")
MsgBox mDict("Sex")
MsgBox mDict("Username")
MsgBox mDict("Password")
Set mDict=Nothing
页:
[1]