尝试使用 VBA 查询 Access 数据库时出现自动化错误
Automation Error when Attempting to Query Access Database Using VBA
我在代码中做了以下 ADODB 对象声明。
Dim OConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Set OConn = New ADODB.Connection
Set rs = New ADODB.Recordset
我想使用以下代码读取 MS Access 数据库文件中的 table 并生成记录集 rs.
'Get the table name from the search results.
tableName = ThisWorkbook.Sheets("PLC Module Data").Cells(2, 9).Value
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM " & tableName
'Set the connection string and open the connection to the Access DB.
OConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Q:\AutoCAD Improvements\PLC IO Utility Docs\PLC IO Spreadsheet
App\PLC IO App\ace_plc.mdb"
OConn.Open
'Open the recordset and error out if nothing is returned
Set rs = OConn.Execute(strSql)
If rs.EOF Then
MsgBox "No matching records found."
rs.Close
OConn.Close
Exit Sub
End If
我已经检查了 Access 文件本身中的查询语句,它工作正常。我总是收到错误
Run-time error'-2147217900 (80040e14)': Automation Error
在线,
Set rs = OConn.Execute(strSql)
如果有人可以查看我的代码并确定为什么会发生这种情况,我们将不胜感激。我在网上看了类似的例子,感觉应该是这样的。
我在 tableName 字符串周围添加了方括号,现在可以使用了。感谢所有反馈。
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM [" & tableName & "]"
我在代码中做了以下 ADODB 对象声明。
Dim OConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Set OConn = New ADODB.Connection
Set rs = New ADODB.Recordset
我想使用以下代码读取 MS Access 数据库文件中的 table 并生成记录集 rs.
'Get the table name from the search results.
tableName = ThisWorkbook.Sheets("PLC Module Data").Cells(2, 9).Value
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM " & tableName
'Set the connection string and open the connection to the Access DB.
OConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Q:\AutoCAD Improvements\PLC IO Utility Docs\PLC IO Spreadsheet
App\PLC IO App\ace_plc.mdb"
OConn.Open
'Open the recordset and error out if nothing is returned
Set rs = OConn.Execute(strSql)
If rs.EOF Then
MsgBox "No matching records found."
rs.Close
OConn.Close
Exit Sub
End If
我已经检查了 Access 文件本身中的查询语句,它工作正常。我总是收到错误
Run-time error'-2147217900 (80040e14)': Automation Error
在线,
Set rs = OConn.Execute(strSql)
如果有人可以查看我的代码并确定为什么会发生这种情况,我们将不胜感激。我在网上看了类似的例子,感觉应该是这样的。
我在 tableName 字符串周围添加了方括号,现在可以使用了。感谢所有反馈。
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM [" & tableName & "]"