请在下面找到代码..
Function Connect_to_db(Byval mfgprt)
Dim cnn,rss
Set cnn = CreateObject("ADODB.Connection")
Set rss = CreateObject("ADODB.recordset")
cnn.ConnectionString = "DSN=QTPDSN;Description=desc;UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"
cnn.open
rss = cnn.Execute (""select UnitPrice from ProductProfilePrices where MfPartNumber ='" + mfgprt + "'")
Connect_to_db=rss(0)
End Function
在这个函数中,如果我用'*'更改Query中的col名称单价,那么它将返回多个值..在这种情况下如何使用rss .....
好像我会这样做(用'*'替换单价),然后在运行时填充错误..在rss数据获取中.. 请同样修改代码.....
谢谢, Galstar
答案 0 :(得分:1)
你可以按名称引用字段,但是第一个rss应该是一个对象,所以使用Set,字符串连接符也是& :
Set rss = cnn.Execute (""select UnitPrice, Quantity " _
& " from ProductProfilePrices where MfPartNumber ='" & mfgprt & "'")
''Let us say that only one row is returned for mfgprt :
varUnitPrice = rss("UnitPrice")
varQuantity = rss("Quantity")
编辑重新评论
Connect_to_db "AAA", Val1, Val2
MsgBox Val1 & " " & Val2
Function Connect_to_db(ByVal mfgprt, ByRef Val1, ByRef Val2)
Dim cnn, rss
Set cnn = CreateObject("ADODB.Connection")
Set rss = CreateObject("ADODB.recordset")
cnn.ConnectionString = "DSN=QTPDSN;Description=desc;" _
& "UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"
cnn.Open
rss = cnn.Execute("select UnitPrice, Quantity " _
& " from ProductProfilePrices where MfPartNumber ='" & mfgprt & "'")
Val1 = rss(0)
Val2 = rss(1)
End Function