简体   繁体   中英

VBA Code execution interrupted trying to close recordset or connection

I try to get Data from an SQL Server into my Excelsheet using ADODB. I've also activated 'Microsoft ActiveX Data Objects 2.8 Library' under References. I have to confess, that I am new to ADODB.

The code below starts fine and the recordset is written correctly into the Excelsheet. However the code throws an error at 'rs.close' saying 'The Code Execution has been interrupted'.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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