I need help in figuring out why below will not work, or find another
approach/solution. I have a workbook with two pivottables that use the
same pivotcache, which in turn is linked to an Access query (i.e.
OutputXXXX)
As the user opens the workbook, a userform is displayed where one can
select a year in a combobox. Depending on the selection, I want the
pivot-cache to be redirected to another output query in the Access
database.
Any thoughts on why below will not work?
Private Sub UserForm_Initialize()
Year_Combobox.List = Array("2004", "2005", "2006", "2007")
End Sub
Private Sub Year_Combobox_Change()
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim NewSheet As Worksheet
Dim Constring As String
Dim DBFile As String
Dim QueryString As String
Dim Year As String
Application.ScreenUpdating = False
' Assign Value to Year
If Year_Combobox.Value = "2004" Then
QueryString = "SELECT * FROM Output2004"
ElseIf Year_Combobox.Value = "2005" Then
QueryString = "SELECT * FROM Output2005"
ElseIf Year_Combobox.Value = "2006" Then
QueryString = "SELECT * FROM Output2006"
ElseIf Year_Combobox.Value = "2007" Then
QueryString = "SELECT * FROM Output2007"
End If
'On Error Resume Next
' Refresh PivotCache in workbook with the selected Year
DBFile = ThisWorkbook.Path & "\CPHNK Måluppföljning.mdb"
Constring = "ODBC;DSN=MS Access Database; DBQ=" & DBFile
With ActiveWorkbook.PivotCaches(1)
.Connection = Constring
.CommandText = QueryString
End With
Sheets("Maluppfoljning").Activate
ActiveSheet.PivotTables("RevenuePivot").RefreshTable
Sheets("YTD_Sum_PivotTable_Sheet").Activate
ActiveSheet.PivotTables("YTD_Sum_Pivot").RefreshTable
'On Error GoTo 0
Sheets("Interface").Activate
Application.ScreenUpdating = True
End Sub