У меня проблема с ошибкой во время выполнения 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**