我正在尝试使用以下代码在 Excel 中的 SQL Server 上打开记录集,但出现3709 - The connection cannot be used to perform this operation. It is either closed or invalid in this context.
错误。我哪里错了?:
编辑:
它现在正在工作,我将更新以下代码:
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Driver={SQL Server};Server=SERVER\SERVER; Database=we_ci_db; Trusted_Connection=Yes"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select * from tblUsers"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)