Hopefully, this message is in the correct forums.
I have pondered over this for 2 days now and cannot find a resolution.
I have a oracle stored procedure which I am pulling into a ADODB recordset. I then create a pivot table based on the recordset.
This all works fine. If however, I apply a filter to the recordset, I can loop through the records in debug mode and see that the returned records are indeed filtered but when I refresh the pivot table, all the unfiltered records are shown.
I have tried refreshing the pivot table but still nothing.
The code is attached if you want to have a look.
Private Sub cmdAllSites_Click()
Dim rst As ADODB.Recordset
Dim db As Connection
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtCache As PivotCache
Dim rsTemp As ADODB.Recordset
Dim SQL As String
Dim i As Long
Set db = New Connection
With db
.CursorLocation = adUseClient
.ConnectionString = "Provider=MSDAORA;" & _
"Data Source=SOURCE;" & _
"User ID=#########;Password=#######;"
db.Open
End With
Set rst = New ADODB.Recordset
SQL = "estores_stock"
rst.Open SQL, db, adOpenStatic, adLockOptimistic
'Filter records depending on choice
rst.Filter = "HospitalCode LIKE 'EDI'"
'make pivot table
Set pvtCache = ActiveWorkbook.PivotCaches.Add(xlExternal)
Set pvtCache.Recordset = rst
Set rsTemp = pvtCache.Recordset
' Delete previous piivot table
Sheets("Sheet1").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
With pvtCache
.CreatePivotTable Range("C2"), "Site Inventory"
End With
Set pvtTable = ActiveSheet.PivotTables("Site Inventory")
'loop through pivotfields to se if they match ADO recordset
For Each pvtField In pvtTable.PivotFields
Debug.Print pvtField.Name
Next
With pvtTable
.SmallGrid = False
With .PivotFields("Code")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Product")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("RH Group")
.Orientation = xlColumnField
.Name = "Blood Group"
.Position = 1
End With
With .PivotFields("Count")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
End With
Set rst = Nothing
End Sub
I have pondered over this for 2 days now and cannot find a resolution.
I have a oracle stored procedure which I am pulling into a ADODB recordset. I then create a pivot table based on the recordset.
This all works fine. If however, I apply a filter to the recordset, I can loop through the records in debug mode and see that the returned records are indeed filtered but when I refresh the pivot table, all the unfiltered records are shown.
I have tried refreshing the pivot table but still nothing.
The code is attached if you want to have a look.
Private Sub cmdAllSites_Click()
Dim rst As ADODB.Recordset
Dim db As Connection
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtCache As PivotCache
Dim rsTemp As ADODB.Recordset
Dim SQL As String
Dim i As Long
Set db = New Connection
With db
.CursorLocation = adUseClient
.ConnectionString = "Provider=MSDAORA;" & _
"Data Source=SOURCE;" & _
"User ID=#########;Password=#######;"
db.Open
End With
Set rst = New ADODB.Recordset
SQL = "estores_stock"
rst.Open SQL, db, adOpenStatic, adLockOptimistic
'Filter records depending on choice
rst.Filter = "HospitalCode LIKE 'EDI'"
'make pivot table
Set pvtCache = ActiveWorkbook.PivotCaches.Add(xlExternal)
Set pvtCache.Recordset = rst
Set rsTemp = pvtCache.Recordset
' Delete previous piivot table
Sheets("Sheet1").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
With pvtCache
.CreatePivotTable Range("C2"), "Site Inventory"
End With
Set pvtTable = ActiveSheet.PivotTables("Site Inventory")
'loop through pivotfields to se if they match ADO recordset
For Each pvtField In pvtTable.PivotFields
Debug.Print pvtField.Name
Next
With pvtTable
.SmallGrid = False
With .PivotFields("Code")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Product")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("RH Group")
.Orientation = xlColumnField
.Name = "Blood Group"
.Position = 1
End With
With .PivotFields("Count")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
End With
Set rst = Nothing
End Sub