使用adologging集到Excel中的数据透视问题

我在最后一行遇到运行时错误1004的问题。 我正在进入excel 2007访问查询。我知道logging集是好的,因为我可以看到字段和数据。 我不知道picotcache是​​在set ptCache行中创build的。 我看到的应用程序,但索引是0.代码如下…

Private Sub cmdPivotTables_Click() Dim rs As ADODB.Recordset Dim i As Integer Dim appExcel As Excel.Application Dim wkbTo As Excel.Workbook Dim wksTo As Excel.Worksheet Dim str As String Dim strSQL As String Dim rng As Excel.Range Dim rs As DAO.Recordset Dim db As DAO.Database Dim ptCache As Excel.PivotCache Set db = CurrentDb() 'to handle case where excel is not open On Error GoTo errhandler: Set appExcel = GetObject(, "Excel.Application") 'returns to default excel error handling On Error GoTo 0 appExcel.Visible = True str = FilePathReports & "Reports SCU\SCCUExcelReports.xlsx" 'tests if the workbook is open (using workbookopen functiion) If WorkbookIsOpen("SCCUExcelReports.xlsx", appExcel) Then Set wkbTo = appExcel.Workbooks("SCCUExcelReports.xlsx") wkbTo.Save 'To ensure correct Ratios&Charts is used wkbTo.Close End If Set wkbTo = GetObject(str) wkbTo.Application.Visible = True wkbTo.Parent.Windows("SCCUExcelReports.xlsx").Visible = True Set rs = New ADODB.Recordset strSQL = "SELECT viewBalanceSheetType.AccountTypeCode AS Type, viewBalanceSheetType.AccountGroupName AS AccountGroup, " _ & "viewBalanceSheetType.AccountSubGroupName As SubGroup, qryAmountIncludingAdjustment.BranchCode AS Branch, " _ & "viewBalanceSheetType.AccountNumber, viewBalanceSheetType.AccountName, " _ & "qryAmountIncludingAdjustment.Amount, qryAmountIncludingAdjustment.MonthEndDate " _ & "FROM viewBalanceSheetType INNER JOIN qryAmountIncludingAdjustment ON " _ & "viewBalanceSheetType.AccountID = qryAmountIncludingAdjustment.AccountID " _ & "WHERE (qryAmountIncludingAdjustment.MonthEndDate = GetCurrent()) " _ & "ORDER BY viewBalanceSheetType.AccountTypeSortOrder, viewBalanceSheetType.AccountGroupSortOrder, " _ & "viewBalanceSheetType.AccountNumber;" rs.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic ' Set rs = db.OpenRecordset("qryExcelReportsTrialBalancePT", dbOpenForwardOnly) **'**********problem here Set ptCache = wkbTo.PivotCaches.Create(SourceType:=XlPivotTableSourceType.xlExternal) Set wkbTo.PivotCaches("ptCache").Recordset = rs** 

你的枢轴caching不是名字“ptCache”,它可能是“PivotCache1”什么的。 其中任何一个都可以工作,前者是首选。

 Set ptCache.Recordset = rs Set wkbTo.PivotCaches(1).Recordset = rs 

我知道这是旧的,但只是一个想法。 我注意到你在同一个子例程中声明了两个rsvariables(一个使用ADODB ,一个使用DAO )。 坦率地说,我很惊讶,编译器没有抓住你,但我会build议尝试一个或另一个,但不是两个(因为你正在初始化rs作为ADODB.RecordSet ,你应该评论出rs = DAO.RecordSet )。