Hello,
I have the code below that is used to feed a pivot table cache with a DAO recordset. The code was from a similar macro used with ADO.
However the code fails at:
Error is: Run-time error '1004' Application-defined or object-defined error
It fails with a DAO recordset but it was working with an ADO recordset Anyone knows why this fails with DAO?
Thank you!
I have the code below that is used to feed a pivot table cache with a DAO recordset. The code was from a similar macro used with ADO.
However the code fails at:
Code:
Set Workbooks(ActWbk).Worksheets(ActSht).Range("MainPivot").PivotTable.PivotCache.Recordset = DAOrs
Error is: Run-time error '1004' Application-defined or object-defined error
It fails with a DAO recordset but it was working with an ADO recordset Anyone knows why this fails with DAO?
Thank you!
Code:
Sub RefreshPivotCache()
Dim aWbkName As String
Dim aShtName As String
Dim MainSourceFile As String
Dim pt As PivotTable
'Variables for DAO
Dim DAOstr As String
Dim SQLstr As String
Dim DAOws As DAO.Workspace
Dim DAOdb As DAO.Database
Dim DAOrs As DAO.Recordset
'To refer back to active workbook/worsksheet
ActWbk = ActiveWorkbook.Name
ActSht = ActiveSheet.Name
MainSource = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
'Create the connection and query strings
DAOstr = "Excel 8.0;HDR=Yes;IMEX=1;"
SQLstr = Range("SQLcommand").Value2
'Launching DAO
Set DAOws = DBEngine.Workspaces(0)
Set DAOdb = DAOws.OpenDatabase(MainSource, False, True, DAOstr) 'write/read
Set DAOrs = DAOdb.OpenRecordset(SQLstr)
Set Workbooks(ActWbk).Worksheets(ActSht).Range("MainPivot").PivotTable.PivotCache.Recordset = DAOrs
Set pt = Workbooks(ActWbk).Worksheets(ActSht).Range("MainPivot").PivotTable
pt.RefreshTable
'Close connection
DAOrs.Close
Set DAOrs = Nothing
DAOdb.Close
Set DAOdb = Nothing
Application.Calculate
End Sub