Pivot Table Connection Relink via VBA causing errors

graphage

Board Regular
Joined
Mar 7, 2002
Messages
87
Good afternoon. I've spent the last few days trying to solve this and can't seem to find an answer. I am hoping someone here can help! :-)

I am using Office 2007.

I have 5 access databases, each having one table in them. I have an Excel Pivot that is linked to one of the 5 Access Database Files and the single table within it. (The tables are large and combined are over 2GB, so I split them into multiple databases).

I am trying to create VBA that will allow me to update the pivot table link based on user selection. The variables will be easy, but I can't even get the root of the code to work!

I recorded a macro and received the following:

Code:
Sub RelinkExistingConnection_MakeSurePickNewFile()

    With ActiveWorkbook.Connections("DrillableSalesReport").OLEDBConnection
        .BackgroundQuery = True
        .CommandText = Array("tblDrillableSalesReport_Raw_Military")
        .CommandType = xlCmdTable
        .Connection = Array( _
        "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=J:\Sales\Regional Reporting\Drillable Sale" _
        , _
        "s Report\Raw\DrillableSalesReport_Raw_Military.accdb;Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";J" _
        , _
        "et OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Glo" _
        , _
        "bal Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=" _
        , _
        "False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=Fal" _
        , "se;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False")
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
        .ServerFillColor = False
        .ServerFontStyle = False
        .ServerNumberFormat = False
        .ServerTextColor = False
    End With
    With ActiveWorkbook.Connections("DrillableSalesReport")
        .Name = "DrillableSalesReport"
        .Description = ""
    End With
    ActiveWorkbook.Connections("DrillableSalesReport").Refresh
End Sub

When I try and run the recorded code (w/o making any modification), the first errors I get are related to the 4 lines that start w/ ".Server". Error is "Run-time error '5': Invalid procedure call or argument". Once I comment them out, the code makes it down to the last line of:

Code:
ActiveWorkbook.Connections("DrillableSalesReport").Refresh

The error is: Run-time error '1004': Application-defined or object-defined error

If I comment out that line, then the code runs to the end. At this point, I can manually right click the data, click Refresh, and then I have the new data. But, if I try and refresh it via vba, I continue to get error 1004. I have also tried this at the end:

Code:
Sheets("DrillableSalesReport").PivotTables("PivotTable2").PivotCache.Refresh

It also generates Run Time error 1004.

Thoughts?

Thank you!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top