ACCESS Design Tips & Code
|
|
ACG Developer Products
|
|
|
Current Tip/Code Sample: Changing the Record Source for a Graph in a Report | |
|
Archive of Tips and Code Samples | |
|
|
|
|
If you have an Access tip or code sample that you'd like to share, mail it to us and we may add to our pages here for other Access Users. |
|
| | Changing the Record Source for a Graph Object in Reports |
A common request is to change and set the Record source for a Microsoft Graph object in an Access report at run time. The MS Graph control does not directly allow you to change its record source at run time, producing an error if you attempt to do so. However there are two solutions that can serve as a work around to allow dynamic graph objects within an Access report.
1) Use a generic query as the row source for the chart
(something that returns some number of rows), and then use either DAO or ADO
depending on what data access method you are using to change the SQL string
for that query before you open the report. Then in your report, in the On
Print event of the section that contains the graph, add code like this:
2) Alternately, you can use a dynamic (run time) record set to fill the
Graph object and set its data series as well. To do so, you fill
the Data Sheet sub object of the Graph object with data that you want
displayed. Then tell Graph to refresh itself and the new data will be
displayed in the Graph, including changes in series, data point and data.
Note however that any settings you have made for colors of bars or pie
pieces etc., will be retained.
The code below shows how to implement this method when it is run in the On
Print event of the section of the report that contains the Graph object. It is shown
using DAO data access code, however the same method will work with ADO data
access methods and with record set returned from SQL Server Views and Stored
Procedures. Note that if you need to change Graph titles and
other objects within the Graph, those changes are not shown here and you
should consult the Graph objects help file in the programming section.
Solution: | |