我目前正在开发一个项目,我需要在Excel工作表上使用SQL查询。出于这个原因,我选择了ADODB记录集。但是我遇到了一个问题,因为在引用记录集时会忽略一些代码行。 有问题的代码如下:
Dim conn As New ADODB.Connection
Dim rs2 As New ADODB.Recordset
DBPath = ThisWorkbook.FullName
sconnect = "Provide=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
conn.Open sconnect
vSql = "SELECT AMOUNT, COUNT(AMOUNT) FROM [MU_v2#7$A1:F192] WHERE TBL = '12.0' AND ROWNAME = '30' AND COLNAME = '45'"
rs2.Open vSql, conn
Debug.Print("Hello")
Debug.Print("The value is: " & rs2(0))
If rs2(1) = 0 Then
ws.Cells(1, 13).Value = "No entry found"
Else
ws.Cells(1, 13).Value = rs2(0)
End If
rs2.Close
当我运行这个时,我只得到“Hello”这一行,然而“值是:X”