简体   繁体   English

VBA代码执行中断,试图关闭记录集或连接

[英]VBA Code execution interrupted trying to close recordset or connection

I try to get Data from an SQL Server into my Excelsheet using ADODB. 我尝试使用ADODB将数据从SQL Server导入到Excelsheet中。 I've also activated 'Microsoft ActiveX Data Objects 2.8 Library' under References. 我还激活了“参考”下的“ Microsoft ActiveX数据对象2.8库”。 I have to confess, that I am new to ADODB. 我不得不承认,我是ADODB的新手。

The code below starts fine and the recordset is written correctly into the Excelsheet. 下面的代码可以正常运行,并且记录集已正确写入Excelsheet。 However the code throws an error at 'rs.close' saying 'The Code Execution has been interrupted'. 但是,代码在“ rs.close”处抛出错误,提示“代码执行已被中断”。

Since this Error Message is very general i wasn't able to find anything regarding this by myself. 由于此错误消息非常笼统,因此我自己无法找到与此有关的任何内容。

I've shut down all other Programms that are accessing the same database on my machine. 我已经关闭了所有其他在我的计算机上访问同一数据库的程序。

Sub Test()

Dim con As ADODB.Connection
Dim conStr As String
Dim SQL As String
Dim rs As Recordset
Dim myWb As Workbook

Set myWb = ThisWorkbook

Set con = New ADODB.Connection
conStr = "Provider=SQLOLEDB;Data Source=*censored*;Initial Catalog=RegMetrics;Trusted_connection=yes;"
con.Open conStr

SQL = "Select * From dbo.fn_Counterparty_RWA()"
Set rs = con.Execute(SQL)
myWb.ActiveSheet.Range("A2").CopyFromRecordset rs

rs.Close
con.Close

End Sub

只需重新启动计算机即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM