ADO计数类型不匹配

时间:2014-05-14 18:49:46

标签: sql excel vba excel-vba

我是指我之前的帖子https://stackoverflow.com/questions/23621564/syntax-of-select-count-in-ado

中的一个

我的Excel文件和另一个正常工作的Excel文件之间有ADO连接

Set adoConn = CreateObject("ADODB.Connection")
adoConn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & CStr("F:\Tools\FDd_v1.3.xlsm") & ";Extended Properties=""Excel 12.0;HDR=Yes"";"

我正在尝试使用Error Type Mismatch

在Excel / VBA中写入此请求
Dim TCode As String
Dim at As Long    
TCode = WS.Range("J" & CStr(c)).Value & WS.Range("L" & CStr(c)).Value
at = "Count * from [Trader$C2:C5000] where [Trader_ID]=" & CStr(TCode)

我也试过了at = "Count * from [Trader$C2:C5000] where [Trader$C2:C5000]=" & CStr(TCode),但也有同样的错误。 [Trader_ID]是列C. Thxx的标题

1 个答案:

答案 0 :(得分:0)

尝试以不同的方式建立连接。在这里,我将返回一个记录集,然后从结果中获取计数。

Sub countRecordset()
    Dim myConnection As String
    Dim myRecordset As ADODB.Recordset
    Dim mySQL As String

    myConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & CStr("F:\Tools\FDd_v1.3.xlsm") & _
               "Extended Properties=Excel 12.0;HDR=Yes"

    Dim TCode As String
    Dim at As Long
    TCode = WS.Range("J" & CStr(c)).Value & WS.Range("L" & CStr(c)).Value

    mySQL = "SELECT * from [Trader$C2:C5000] where [Trader_ID]=" & CStr(TCode)

    Set myRecordset = New ADODB.Recordset
    myRecordset.Open mySQL, myConnection, adOpenUnspecified, adLockUnspecified

    MsgBox myRecordset.Fields.Count
End Sub