问个VB的问题:在VB中怎样将SQL的查询结果赋值给一个变量???
举个例子:
select sum(" + adoRecordset.Fields(ColIndex).Name + ") from ad
然后怎样才能把这个查询结果赋值给一个变量???
谢谢!!!
[解决办法]
Dim sql As String
Dim aa As Integer
MsgBox adoRecordset.Fields(ColIndex).Name
sql = "select sum(" + adoRecordset.Fields(ColIndex).Name + ") as aa from ad "
' adoConnection.Execute sql
'Set adoRecordset = adoConnection.Execute("select sum(" + adoRecordset.Fields(ColIndex).Name + ")as aa from ad")
' MsgBox aa
Set adoRecordset = Nothing
adoRecordset.Open sql, adoConnection, 1, 3
MsgBox "合计:" & adoRecordset.Fields.Item(0).Value
[解决办法]
直接赋值就行
dim result as intsql = "select sum(" + adoRecordset.Fields(ColIndex).Name + ") as counter from ad" rs.open sql, myconn, 3,3,1 '假设rs和myconn已经定义好和初始化好。result = rs("counter") '在这里赋值。