I am trying to run the below SQL in VBA, but I am getting an "operation is not allowed when the object is closed" run-time error.
This SQL script works in SSMS, so it must be something I am missing in the VBA (which isn't my strong point.).
If anyone know where I have gone wrong, would be highly appreciated.
Set Conn = CreateObject("ADODB.Connection")
Set recset = CreateObject("ADODB.Recordset")
'set parameters
ServerSource = Sheets("Servers + Databases").Range("F27").Value 'choose the server the database is located
Year = Sheets("Region").Range("C4").Value 'choose the year
Period = Sheets("Region").Range("C5").Value 'choose the period
'insert server name and database name
sConnect = "Provider=SQLOLEDB.1;" & _
"Password=ExcelRep0rt;" & _
"User ID=ExcelReport;" & _
"Data Source=" & ServerSource & ";" & _
"Use Encryption for Data=False"
Conn.Open sConnect
'SQL query
SQLQry = " DECLARE @table table(dbname sysname COLLATE Latin1_General_CI_AS)" & _
" INSERT INTO @table(dbname)" & _
" SELECT NAME COLLATE Latin1_General_CI_AS FROM sys.databases where name like '%AccountsLive'" & _
" DECLARE @Sql NVARCHAR(MAX) = NULL;" & _
" SELECT @Sql = COALESCE(@Sql + ' UNION ALL ' + CHAR(13) + CHAR(10), '' ) +" & _
" 'SELECT ST_ALOC_POINTER COLLATE DATABASE_DEFAULT AS ''Cust_Code'', ST_TRANTYPE COLLATE DATABASE_DEFAULT AS ''Transaction'', ST_HEADER_REF COLLATE DATABASE_DEFAULT AS ''Reference'', ST_GROSS AS ''Local_Gross'', ST_CURRENCYCODE COLLATE DATABASE_DEFAULT AS ''Currency'', ST_CURR_VALU AS ''Foreign_Gross'', ST_DESCRIPTION COLLATE DATABASE_DEFAULT AS ''Description'' FROM '" & _
" + QUOTENAME(dbname) COLLATE Latin1_General_CI_AS + '.dbo.SL_TRANSACTIONS" & _
" WHERE ST_YEAR = '' & Year & ''" & _
" and ST_PERIODNUMBER = '' & Period & ''" & _
" and ST_ALOC_POINTER like ''%LC%''" & _
" and ST_GROSS <> ''0''" & _
" and ST_TRANTYPE <> ''INV''" & _
" and ST_TRANTYPE <> ''CRN''" & _
" Union ALL" & _
" SELECT ST_ALOC_POINTER COLLATE DATABASE_DEFAULT AS ''Cust_Code'', ST_TRANTYPE COLLATE DATABASE_DEFAULT AS ''Transaction'', ST_HEADER_REF COLLATE DATABASE_DEFAULT AS ''Reference'', ST_GROSS AS ''Local_Gross'', ST_CURRENCYCODE COLLATE DATABASE_DEFAULT AS ''Currency'', ST_CURR_VALU AS ''Foreign_Gross'', ST_DESCRIPTION COLLATE DATABASE_DEFAULT AS ''Description'' FROM '" & _
" + QUOTENAME(dbname) COLLATE Latin1_General_CI_AS + '.dbo.SL_TRANSACTIONS" & _
" WHERE ST_YEAR = '' & Year & ''" & _
" and ST_PERIODNUMBER = '' & Period & ''" & _
" and ST_ALOC_POINTER like ''%CR%''" & _
" and ST_GROSS <> ''0''" & _
" and ST_TRANTYPE <> ''INV''" & _
" and ST_TRANTYPE <> ''CRN'''" & _
" FROM @table" & _
" EXEC( @Sql );"
'import table - choose range of where to put the table
Set recset = New ADODB.Recordset
recset.Open SQLQry, Conn
Range("B10").CopyFromRecordset recset
recset.Close
Conn.Close
Set recset = Nothing
With the help of @QHarr, adding SET NOCOUNT ON
at the start of the SQL solved this particular issue
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.