我在最后一行遇到运行时错误 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**