将 VBA 连接到 Oracle 时连接关闭时出错
我在从 Excel 电子表格中的 VBA 代码连接到 Oracle 数据库时遇到错误。我运行的查询工作正常,但当我尝试关闭连接时,出现以下错误:
运行时错误 3265:在与请求的名称或序号相对应的集合中找不到项目。
我的代码副本如下。错误发生在“cn.close”行上。任何帮助将不胜感激!
Sub GetData()
Dim cn As New ADODB.Connection
comm As New ADODB.Command
rs As New ADODB.Recordset
On Error GoTo errhandler:
cn.ConnectionString = "DSN=XXX;Uid=XXX;Password=XXX;"
cn.Open
comm.CommandType = adCmdText
comm.CommandText = "Select * from XXX where rownum < 10;"
Set comm.ActiveConnection = cn
rs.ActiveConnection = cn
rs.Open comm
Sheets("Sheet1").Range("a1").Offset(1, 0).CopyFromRecordset rs 'copy the records
rs.Close
cn.Close
errhandler:
Debug.Print (Err.Description)
Debug.Print "Error# " & cn.Errors(0).NativeError & ": " & cn.Errors(0).Description
Stop
End Sub
I'm encountering an error when connecting to an Oracle DB from VBA code in an Excel Spreadsheet. The query that I run works fine, but when I try to close the connection, I get the following error:
Runtime Error 3265: Item cannot be found in the collection corresponding to the requested name or ordinal.
A copy of my code is below. The error occurs on the line "cn.close". Any help would be greatly appreciated!
Sub GetData()
Dim cn As New ADODB.Connection
comm As New ADODB.Command
rs As New ADODB.Recordset
On Error GoTo errhandler:
cn.ConnectionString = "DSN=XXX;Uid=XXX;Password=XXX;"
cn.Open
comm.CommandType = adCmdText
comm.CommandText = "Select * from XXX where rownum < 10;"
Set comm.ActiveConnection = cn
rs.ActiveConnection = cn
rs.Open comm
Sheets("Sheet1").Range("a1").Offset(1, 0).CopyFromRecordset rs 'copy the records
rs.Close
cn.Close
errhandler:
Debug.Print (Err.Description)
Debug.Print "Error# " & cn.Errors(0).NativeError & ": " & cn.Errors(0).Description
Stop
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 cn.Close 行之后,没有什么可以阻止执行继续进入错误处理程序,因此您的错误可能来自错误处理程序本身(因为处理程序试图引用不存在的 Err 对象)。
There's nothing to stop execution from continuing into your error handler after the cn.Close line, so it's possible your error is coming from the error handler itself (because the handler is trying to reference a non-existant Err object).