views:

629

answers:

1

I tried to record the code to update a pivot sourcedata which gave me this:

ActiveSheet.PivotTableWizard SourceType:=xlExternal, _
    SourceData:=QueryArry1, _
    Connection:=Array( _
        Array("ODBC;DSN=MS Access Database;DBQ=" & DBDir & "\" & DBName & ";"), _
        Array("DefaultDir=" & DBDir & ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;") _
    )

But this doesn't even allow me to specify WHICH pivot table I want to update... or even do what I really want to do which is update the pivotcache so that all pivot tables using that same source are updated.

So what is a good way to update the sourcedata?

Thanks

EDIT:

But I even get the "application-defined or object-defined error" error with something as simple as:

str = Sheets("Totals").PivotTables("PivotTable2").PivotCache.CommandText
Sheets("Totals").PivotTables("PivotTable2").PivotCache.CommandText = str

And I did double check that my pivot table is still hitting the live data and refreshing it still works... yet I can't set the command string to what it is currently? So bizarre.

Thanks

+3  A: 

The PivotCaches are accessible through Workbooks. You can list all your current caches with the following sub:

Option Explicit

Private Sub listCaches()
    Dim selectedCache As PivotCache

    For Each selectedCache In ThisWorkbook.PivotCaches
        Debug.Print selectedCache.Index
        Debug.Print selectedCache.Connection
    Next selectedCache

End Sub

You can access the connection you want to edit with:

ThisWorkbook.PivotCaches(yourIndex).Connection

Note: After changing the Connection you should call:

ThisWorkbook.PivotCaches(yourIndex).Refresh

Edit: Instead of changing the SourceData you can change the CommandText. That should have the same effect. The following code worked for me:

ThisWorkbook.PivotCaches(1).CommandText = "SELECT movies.title, movies.rating, movies.comments FROM `C:\Folder\moviesDB`.movies movies"
ThisWorkbook.PivotCaches(1).Refresh

This code also updated my SourceData.

Edit2: Changing CommandText throgh PivotTable:

Sheets("mySheet").PivotTables("PivotTable1").PivotCache.CommandText = "SELECT movies.title as meh, movies.rating, movies.comments FROM `C:\Folder\moviesDB`.movies movies"
Sheets("mySheet").PivotTables("PivotTable1").PivotCache.Refresh

Note: moviesDB is a .mdb file and movies is the table/query

Note2: It might also help you to Debug.Print the working CommandText before changing it. This should give you a template for your new CommandText.

marg
The Connection is fine, I'm trying to change the SourceData... but I get "Application-defined or object-defined error" when I try to "ThisWorkbook.PivotCaches(2).SourceData = QueryArry1"... I'm starting to think that this type of SourceData reference is read-only and can't be used to set the value. I've tried several different forms of QueryArry1, including "QueryArry1=ThisWorkbook.PivotCaches(2).SourceData"... I always get the same error
Dan
I am not sure what the contents of your QueryArry are but I guess you want to change the Query that is send to the database. Changing CommandText worked for me. I added a sample code in my answer.
marg
The Pivot caches keep changing their index... So I'm trying the command: "Sheets("Totals").PivotTables("PivotTable1").PivotCache.CommandText = "SELECT * FROM Totals"" but it gives me the error: "application-defined or object-defined error"
Dan
I was able to change CommandText thorgh PivotTable with the code I added in my answer. I was also able to replicate your Error by trying to set CommandText to a wrong Query like "SEEELEKT * From ..." or by not including the path to the database in the Query.
marg
Still have unexplainable issues... see my edit. thanks for your work so far... hopefully I am almost there.
Dan
I renamed a sheet in Totals and added 2 PivotTables in it and executed the code from your edit with no problems. But I had to change str to bla because str is a Excel Function.
marg
Thanks for your help. While it still isn't working, you've really helped me refine my issue and approach. I've made a new thread addressing the specific error I'm getting here: http://stackoverflow.com/questions/1883440/excel-vba-application-defined-or-object-defined-error
Dan