Problem refreshing pivot using ADODB recordset

ribhead

New Member
Joined
Mar 25, 2013
Messages
4
First,
My apologies if this is posted somewhere else with a solution but all my searches have come up with no specific solution(s). 2nd, I am not an expert programmer but I know enough to do some automation with the basics. I have been able to create a pivot table in Excel 2016 using vba with the source data in SQL. When I try to refresh the pivot cache/pivot table I get the 1004 Error "Problems Obtaining Data". I am able to dump the data in a worksheet with no issues so it's not a record count issue. I assume my vba code is incorrect somewhere and would prefer to know that prior to just dumping the data in a table with a pivot linked to it. Below is my code. Most of the answers I found said there was an issue with Excel and I could do some Registry work but before that I want to make sure it's nothing dumb. I've been married 3 times so being yelled at and verbally abused for things being messed up are ok with me.:biggrin: Any help would be outstanding - Thank you.


Code fails here: ActiveSheet.PivotTables("TBACTUALS").PivotCache.Refresh
I toggled the .EnableRefresh = True/False and it still fails....

Sub RefreshADOPivot()
Dim pvtT As PivotTable
Dim pvtC As PivotCache
Dim intindex As Integer
Code:
Sheets("Test").Activate
 
Set pvtT = ActiveSheet.PivotTables("TBACTUALS")
If cnt.State = adStateClosed Then
    cnt.Open strConn
End If
intindex = ActiveSheet.PivotTables("TBACTUALS").PivotCache.Index
CalYr = CInt(Mid(Range("CREATESCENARIO")Value, 5, 4))
pd = CInt(Mid(Range("CREATESCENARIO").Value, 10, 2)) - 1
bua = Range("BUA").Value
If cnt.State = adStateClosed Then
    cnt.Open strConn
End If
With cmd
    .ActiveConnection = strConn
    .CommandType = adCmdStoredProc
    .CommandText = "usp_TBActuals"
   .Parameters.Append .CreateParameter("@calyr", adInteger, adParamInput, 4, CalYr)
    .Parameters.Append .CreateParameter("@pd", adInteger, adParamInput, 2, pd)
    .Parameters.Append .CreateParameter("@bua", adVarChar, adParamInput, 4, bua)
Set rst = .Execute
End With
If Not (rst.EOF Or rst.BOF) Then
pvtT.PivotCache.EnableRefresh = True
Set ActiveWorkbook.PivotCaches(intindex).Recordset = rst
[COLOR=#ff0000]ActiveSheet.PivotTables("TBACTUALS").PivotCache.Refresh[/COLOR]
End If
If cnt.State = adStateOpen Then
    rst.Close
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing
End If
End Sub
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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