Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-SQL查询错误:关闭对象时不允许操作_Sql_Vba_Excel - Fatal编程技术网

Excel VBA-SQL查询错误:关闭对象时不允许操作

Excel VBA-SQL查询错误:关闭对象时不允许操作,sql,vba,excel,Sql,Vba,Excel,我有一个VBA脚本,它调用SSMS运行查询并将结果粘贴到Excel中。在此之前,我在另外两个查询中使用了相同的代码,它们都可以工作,但由于某些原因,这一个给出了错误 我最初认为这是由于临时表的原因,所以我在开头添加了set nocount,但没有帮助。任何提示或建议都将不胜感激 错误消息: 运行时错误“3704”: 关闭对象时不允许执行操作 Sub Test() 'Setup variables Dim cnn As New ADODB.Connection Dim rst As New AD

我有一个VBA脚本,它调用SSMS运行查询并将结果粘贴到Excel中。在此之前,我在另外两个查询中使用了相同的代码,它们都可以工作,但由于某些原因,这一个给出了错误

我最初认为这是由于临时表的原因,所以我在开头添加了set nocount,但没有帮助。任何提示或建议都将不胜感激

错误消息:

运行时错误“3704”:

关闭对象时不允许执行操作

Sub Test()

'Setup variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim SOURCE As String
Dim DATABASE As String
Dim QUERY As String
Dim intColIndex As Integer

'Assign variables
SOURCE = [Data Source]
DATABASE = [Database]

QUERY = "set nocount on"
QUERY = QUERY + " declare @Variable1 int"
QUERY = QUERY + " declare @Variable2 int"
QUERY = QUERY + " declare @Variable3 datetime"
QUERY = QUERY + " declare @Variable4 datetime"
QUERY = QUERY + " select @Variable1 = [xxxx]"
QUERY = QUERY + " select @Variable2=(select Field1 from Table1 where Field2 = @Variable1)"
QUERY = QUERY + " select @Variable3=min(Variable3) from Table2 where Field3=@Variable2"
QUERY = QUERY + " select @Variable4=max(Field4) from Table2 where Field3=@Variable2"
QUERY = QUERY + " select distinct b.Field5, b.Field10 into #Temp1"
QUERY = QUERY + " from Table3 p(nolock)"
QUERY = QUERY + " inner join Table4 b(nolock) on b.Field5 = p.fkField5"
QUERY = QUERY + " inner join Table5 cp (nolock) on cp.Field6 = p.Field11"
QUERY = QUERY + " where Field3=@Variable2 "
QUERY = QUERY + " select Field3,Field9,Variable3,Field4 into #Temp2 from Table2 cf (nolock)"
QUERY = QUERY + " inner join Table6 ac (nolock) on ac.Field7=cf.Field3"
QUERY = QUERY + " where Variable3 >= @Variable3 - 365 and Field4 < @Variable4 + 1"
QUERY = QUERY + " and ac.Field8 <>4"
QUERY = QUERY + " select distinct cp.Field3,Field9,convert(varchar(10),Variable3,101) Variable3,convert(varchar(10),Field4,101) Field4"
QUERY = QUERY + " from Table5 cp (nolock)"
QUERY = QUERY + " inner join Table3 p (nolock) on p.Field11=cp.Field6"
QUERY = QUERY + " inner join Table4 b (nolock) on b.Field5 = p.fkField5"
QUERY = QUERY + " inner join #Temp1 bb on bb.Field5=b.Field5"
QUERY = QUERY + " inner join #Temp2 oth on oth.Field3=cp.Field3"
QUERY = QUERY + " drop table #Temp1"
QUERY = QUERY + " drop table #Temp2"

'Run the query and paste results into Cell X11 on the first tab of the workbook
    ConnectionString = "Provider=SQLOLEDB;Data Source=" & SOURCE & "; Initial Catalog=" & DATABASE & "; Integrated Security=SSPI;"

    cnn.Open ConnectionString

    cnn.CommandTimeout = 900

    StrQuery = QUERY

    rst.Open StrQuery, cnn

    Sheets(1).Range("X11").CopyFromRecordset rst
'Add the column headers
For intColIndex = 0 To rst.Fields.Count - 1
    Range("X10").Offset(0, intColIndex).Value = rst.Fields(intColIndex).Name
Next 

End Sub