Microsoft KB Archive/154582

= ACC: How to Use Automation to Change a Graph Chart Type =

Article ID: 154582

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q154582



Advanced: Requires expert coding, interoperability, and multiuser skills.



SUMMARY
This article shows you how to use Automation to change the chart type of a Microsoft Graph object.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: A demonstration of the technique used in this article can be seen in the sample file, Grphsm97.exe. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:

186855 ACC97: Microsoft Access 97 Sample Graphs Available in Download Center



MORE INFORMATION
You can use Automation to change the chart type of a Microsoft Graph. However, changing a graph from one type to another automatically resets properties of the graph that do not apply to the new graph type. For example, Trendlines apply to a Two Dimensional Column graph, but do not apply to a Pie chart. Therefore, changing the type from a Column to a Pie drops the Trendline.

Example: Changing a Chart Type
The following example shows you how to produce a chart in the Northwind.mdb database and change a graph's chart type to a 3-Dimensional Pie:  Open the sample database Northwind.mdb. Create a new form not based on any table or query in Design view. On the Insert menu, click Chart. Click and drag a chart to the form. In the first dialog box of the Chart Wizard, under View, click Queries, and then select the "Employee Sales By Country" query. Click Next. In the next dialog box, add the Country and SaleAmount fields to the Fields For Chart box. Click Finish. Your graph appears in the form. On the View menu, click Properties. Select the Graph so that you are viewing the graph object's properties. Set the Name property to MyGraph.</li> Add a command button to the form with the following properties:

Command button: MyButton

Name: MyButton

Caption: Pie Chart

OnClick: [Event Procedure]

</li> On the View menu, click Code to view the form's module.</li> On the Tools menu, click References, and then check the Microsoft Graph 8.0 Object Library (in Microsoft Access 7.0, check the Microsoft Graph 5.0 Object Library).</li>  Type the following procedure into the module. Sub MyButton_Click Dim GraphObj As Object Set GraphObj = Me![MyGraph].Object.Application.Chart GraphObj.Type = xl3DPie End Sub NOTE: To view other chart types you can set through Visual Basic for Applications, click Object Browser on the View menu and search the Microsoft Graph object library for "Constants". </li> Switch the form to Form view. When prompted for a beginning and ending date, enter 1/1/94 and 1/1/95 respectively. Click the command button. Note that the chart changes to a 3-dimensional pie shape.</li></ol>

Example: Determining Chart Type of Graph
The following example determines if the chart type of the graph in the earlier example is a 2-Dimensional Bar: <ol> Open any module in Design view. On the Tools menu, click References, and then check the Microsoft Graph 8.0 Object Library reference (in Microsoft Access 7.0, check the 5.0 Object Library reference). Click OK.</li>  Using the form created in the first example, change the code of the command button to the following: Dim GraphObj As Object Set GraphObj = Me![MyGraph].Object.Application.Chart If GraphObj.Type = xlBar Then 'xlBar equals 2 Msgbox "The graph is a 2-D Bar chart" Else Msgbox "The graph is NOT a 2-D Bar chart" End If                   </li> Switch the form to Form view. When prompted for a beginning and ending date, enter 1/1/94 and 1/1/95 respectively. Click the command button. Note the message box that states the graph is not a 2-Dimensional bar chart.</li></ol>

<div class="references_section">