|
本帖最后由 bluestone_yang 于 2013-9-21 23:00 编辑
问题概述:
本人通过ADO链接操作EXCEL,第一次运行无误,第二次出现以下问题:“错误:-2147467259(80004005)用于查看已链接的 Micorosoft Excel 工作表的连接,已经失去。”
初始状态下的语句如下(当然略过了很多部分了):
- Dim adoCN As Object
- Dim sqlP$, sqlS$, sqlI_Initial$, sqlI_Final$, sql$
- Set adoCN = CreateObject("adodb.connection")
- adoCN.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1;hdr=yes';data source=" & Workbooks(wbI).FullName
- sqlP = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Purchase'as [Transaction Type],[Bill Date] from [" & shP & "$] where [Bill Date]>='" & firstDay & "' and [Bill Date]<='finalDay'"
- sqlS = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Sales' as [Transaction Type],[Bill Date] from [" & shS & "$] where [Bill Date]>='" & firstDay & "' and [Bill Date]<='finalDay'"
- sqlI_Initial = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Initial Invetory'as [Transaction Type],[Inventory Date] from [" & shI & "$] where [Inventory Date]='" & firstDay & " '"
- sqlI_Final = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Final Invetory' as [Transaction Type],[Inventory Date] from [" & shI & "$] where [Inventory Date]='" & finalDay & " '"
- sql = sqlP & "union all " & sqlS & "union all " & sqlI_Initial & "union all " & sqlI_Final
- Sheets("Process").[A2].CopyFromRecordset adoCN.Execute(sql)
- Dim objPivotCache As Object
- Set objPivotCache = Workbooks(wbI).PivotCaches.Add(SourceType:=xlExternal)
- Set objPivotCache.Recordset = adoCN.Execute(sql)
- objPivotCache.CreatePivotTable TableDestination:=Workbooks(wbI).Sheets("Balance").Range("A3"), TableName:="Balance"
复制代码 第一次运行没问题,第二次就出现上面的情况了
解决方法很简单
添加
然后就可以了,最后如下:- Dim adoCN As Object
- Dim sqlP$, sqlS$, sqlI_Initial$, sqlI_Final$, sql$
- Set adoCN = CreateObject("adodb.connection")
- adoCN.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1;hdr=yes';data source=" & Workbooks(wbI).FullName
- sqlP = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Purchase'as [Transaction Type],[Bill Date] from [" & shP & "$] where [Bill Date]>='" & firstDay & "' and [Bill Date]<='finalDay'"
- sqlS = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Sales' as [Transaction Type],[Bill Date] from [" & shS & "$] where [Bill Date]>='" & firstDay & "' and [Bill Date]<='finalDay'"
- sqlI_Initial = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Initial Invetory'as [Transaction Type],[Inventory Date] from [" & shI & "$] where [Inventory Date]='" & firstDay & " '"
- sqlI_Final = "select [Organization Name],[Distributor Name],[Distributor Code],[Client Code],[Distributor ProductCode],[Distributor ProductName],[Distributor Product Unit],[Distributor Product Quantity],'Final Invetory' as [Transaction Type],[Inventory Date] from [" & shI & "$] where [Inventory Date]='" & finalDay & " '"
- sql = sqlP & "union all " & sqlS & "union all " & sqlI_Initial & "union all " & sqlI_Final
- Sheets("Process").[A2].CopyFromRecordset adoCN.Execute(sql)
- Dim objPivotCache As Object
- Set objPivotCache = Workbooks(wbI).PivotCaches.Add(SourceType:=xlExternal)
- Set objPivotCache.Recordset = adoCN.Execute(sql)
- objPivotCache.CreatePivotTable TableDestination:=Workbooks(wbI).Sheets("Balance").Range("A3"), TableName:="Balance"
- adoCN.Close
- Set adoCN = Nothing
复制代码
不清楚能否解决所有的这类情况,至少我的问题解决了;
网上搜索的一下,如果这个方法不能解决,大家请移步参考:
[紧急求助] VBA使用SQL查询,ADODB连接出现莫名错误,救命啊!
[求助] excel通过ado操作工作表的问题
请教:recordset超多次重复OPEN占用内存的问题
|
|