So I have a ms query with a parameter (let's call it "qry_Accounting") and I linked this parameter to a field that is linked to a combobox. This way I can filter my data with the combobox.
I then created a Pivot Table based on the ms query. Normally, I should be able to see the data linked to the item selected in the combobox. I can see that when I change the item selected in the combobox the data changes in "qry_Accounting". I had to create a button, so that when I change the item selected and click on the button it will refresh the pivot table.
My problem is I always have the data from the previous selected item in my pivot table, and only that data. I really don't understand what happens. If I create a new pivot table it will have the correct data, but the behaviour continues when I change the item, I still always keep the previous data and I don't get the new data.
One more thing, If I manually refresh the table it will work. It will work even for multiple pivot tables when I refresh one of them if they are all linked to the same qry_Accounting table.
How can I have the correct data showing without asking the user to manually refresh all the time?
PS : I already changed the property "Number of items to retain per field" to "none".
Following some questions in the comment here is more of my code (When clicking on the button to apply the combobox change) :
Sub Button5_Click()
'If the buffer place (O4) is different from the combo linked cell (F4) then
'assign the value from F4 to O4.
'the table will then be updated because as soon as O4 value changes the
'table is updated (the parameter is taken from O4)
If Sheets("base_pivot").Range("O4").Value <> Sheets("base_pivot").Range("F4").Value Then
Sheets("base_pivot").Range("O4").Value = Sheets("base_pivot").Range("F4").Value
'We wait 5 seconds to avoid access denied problems
Application.Wait (Now + #12:00:05 AM#)
'we ask for a refresh of the pivot tables explicitely
RefreshPivotTables ActiveWorkbook
'we ask for a refresh of all data.
ActiveWorkbook().RefreshAll
End If
End Sub
Sub RefreshPivotTables Code
Sub RefreshPivotTables(wb As Workbook)
Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
For Each ws In wb.Worksheets
For Each pt In ws.PivotTables
pt.PivotCache().Refresh
pt.RefreshTable
Next
Next
End Sub
Other than that everything is automatic. The ms query has a parameter, so as soon as the O4 value is updated, the new data is loaded in the table. So then the only thing to do it to refresh the pivottable.