I'm having a problem with runtime error 1004 at the last line. I'm bringing in an access query into excel 2007. I know the recordset is ok as I can see the fields and data. Im not sure about the picotcache was created in the set ptCache line. I see the application, but the index is 0. Code is below...
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**