运行时错误13类型不匹配

时间:2014-09-27 04:25:23

标签: excel excel-vba excel-2010 vba

尝试为Ms Excel VBA运行选择字符串时出现自动化错误

代码如下:

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strsql As String

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

Set cn = New ADODB.Connection

Application.ScreenUpdating = False

DBPath = "Path of Access DB HERE"
DBProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dbparam = DBProvider & "data source =" & DBPath

a = Sheets(1).ComboBox2.Value

cn.Open dbparam
With rs
    .Open "Qry_WBT", cn, adOpenDynamic, adLockOptimistic, adCmdTable
    ***.Open "Select * From " & [Qry_WBT] & " WHERE " & "[WBT Name] = '" & a & "'", cn, adOpenKeyset, adLockOptimistic, adCmdTable***
    If rs.EOF = True Then
        MsgBox "No Data in selected field", vbOKOnly
        Exit Sub
    End If
End With

错误出现在.open"选择等等等等等等等等。节

1 个答案:

答案 0 :(得分:0)

克里斯是对的。你不能打开它两次 这就是我认为3705的原因。
我认为你不需要 Qry_WBT 的括号,所以请一试:

Dim mysource As String
mysource = "Select * From Qry_WBT WHERE [WBT Name] = '" & a & "'"
With rs
    .Open mysource, cn, adOpenKeyset, adLockOptimistic, adCmdTable
    '~~> rest of the code
End With