Unable to update chart using VBA when data is updated

Copper Contributor

I have a spreadsheet with a chart that I can't get to refresh using code.  The data comes from a SQL Server stored procedure, and I’m actually using the chart on an Access report, which requires both the data and the chart to be refreshed on each page of the report.  I have attached an image of the chart in an unrefreshed state, after the data has been refreshed, and if you look closely you will see that the X axis categories do not match the "Stage" in the table.  My VBA code refreshes the data fine.  I am manipulating the Excel instance from within Access using variations of the code below.  By making the Excel object visible I can see the effect of each command, and the data refreshes fine.  But I have been unable to find a VBA command that will successfully refresh the graph, and I’ve tried everything I can think of.  Manually, I can click on a line on the chart, choose “Select Data”, and as soon as I click on one of the ranges the chart is miraculously refreshed.  But trying to do the same thing in code does nothing.

 

gObjExcel.Workbooks("TissueSLGraph.xlsx").Connections("SoilTestStu").OLEDBConnection.CommandText = Array("EXEC dbo.TissueSLGraph @Yr = " & CStr(Me.Yr) & ", @FldNo = " & CStr(Me.FieldID) & ", @Crop = " & CStr(Me.TissueCropID) & "")

       gObjExcel.Workbooks("TissueSLGraph.xlsx").Connections("SoilTestStu").Refresh

       gObjExcel.Workbooks("TissueSLGraph.xlsx").Sheets("Graph").Select

      

       For Each myChart In gObjExcel.ActiveSheet.ChartObjects

           myChart.Chart.Refresh

       Next

       'gObjExcel.Visible = True

      

       gObjExcel.ActiveSheet.ChartObjects("Chart 1").Activate

      

       'gObjExcel.ActiveChart.FullSeriesCollection(2).Select

       'gObjExcel.ActiveChart.FullSeriesCollection(2).Values = "=TissueSLGraph.xlsx!Diff"

      

I’m stumped, and consider the fact that the Chart.Refresh command does not work an Excel bug.  Any help appreciated!

0 Replies