尝试为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"选择等等等等等等等等。节
答案 0 :(得分:0)
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