Another approach would be to open the form or report the chart is embedded in two steps. In the example below I am using a report, but it works just as fine with forms:
- First step: open report in design view but hidden mode. Now the chart
row source can be edited (because you are in design view) but the
process is invisible (because you are in hidden mode).
- Second step: save and close hidden report and re-open it now in a
"visible" mode
'report name
strReportmName = "SomeReportName"
'open report in design view but hidden
DoCmd.OpenReport strReportmName , acViewDesign, , , , acHidden
'edit chart RowSource
strSQL = "TRANSFORM Sum(Cabecas) AS SomaDeCabecas " & _
"SELECT Data "
...etc...
"PIVOT Categoria In (" & Chr(34) & strTitColunas & Chr(34) & ")"
'update chart RowSource
Reports![SomeReportName].Controls![SomeChartName].RowSource = strSQL
'Save report with edited RowSource
DoCmd.Close acReport, strReportmName , acSaveYes
're-open it in normal, visible mode
DoCmd.OpenReport strReportmName , acViewPreview