1

我在最后一行遇到运行时错误 1004 的问题。我将访问查询引入 excel 2007。我知道记录集没问题,因为我可以看到字段和数据。我不确定 picotcache 是在 set ptCache 行中创建的。我看到了应用程序,但索引为 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**
4

4 回答 4

0

您的 Pivot Cache 不是名称“ptCache”,它可能是“PivotCache1”或其他名称。这些中的任何一个都可以工作,前者是首选。

Set ptCache.Recordset = rs

Set wkbTo.PivotCaches(1).Recordset = rs
于 2010-05-05T13:53:09.117 回答
0

我知道这是旧的,但只是一个想法。我注意到您在同一个子例程中声明了两个rs变量(一个 usingADODB和一个 using )。DAO坦率地说,我很惊讶编译器没有为你捕捉到这一点,但我建议尝试其中一个,但不要同时尝试两者(因为你初始化rsADODB.RecordSet,你可能应该注释掉rs = DAO.RecordSet)。

于 2012-03-12T22:56:24.057 回答
0

MHosseinput 可能已经在那里做了一些事情。多年前我在 excel 中完成了此操作,我注意到我将 sql 拆分为块并将一个或多个数组传递给枢轴对象。当然,没有评论解释我为什么这样做,我不记得了,但它确实有效。

Set oPivCache = oWb.PivotCaches.Add(SourceType:=xlExternal) 
oPivCache.Connection = "OLEDB;" & sConnStr
oPivCache.CommandType = xlCmdSql oPivCache.CommandText = Array( _
    Array(LEFT(sqry, 200)), _
    Array(Mid(sqry, 201, 200)), _
    Array(Mid(sqry, 401, 200)), _
    Array(Mid(sqry, 601, 200)), _
    Array(Mid(sqry, 801, 200)), _
    Array(Mid(sqry, 1001, 200))) 
oPivCache.CreatePivotTable TableDestination:=oPVSht.Range("A1"), TableName:=sPvName
于 2022-02-23T21:19:07.400 回答
0

我认为这个错误上升是因为你的“strSQL”太长了;使用更简单的 strSQL 运行您的代码,但我不知道如何解决长 strSQL。

于 2020-09-17T20:53:37.497 回答