This article shows two methods that you can use to work with Microsoft Graph objects. One method demonstrates how to use Automation to change the chart type of a Microsoft Graph object. The second method shows how to determine the graph type.
You can use Automation to change the chart type of a Microsoft Graph object. 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 not to a Pie chart. Therefore, changing the type from a Column to a Pie drops the trendlines.
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 3-Dimensional Pie:
- Start Microsoft Access and 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 onto the form.
- In the first dialog box of the chart wizard, on the View menu, click Queries, select Employee Sales By Country, and then click Next.
- In the next dialog box, add the Country and SaleAmount fields to the Fields For Chart box, and then 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.
- Add a command button to the form with the following properties:
Command button:
-----------------
Name: MyButton
Caption: Pie Chart
OnClick: [Event Procedure]
- On the View menu, click Code to view the form's module.
- On the Tools menu, click References, select Microsoft Graph 9.0 Object Library, and then click OK.
- 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 in Visual Basic for Applications, click Object Browser on the View menu and search the Microsoft Graph object library for "Constants."
- Switch the form to Form view. When prompted for a beginning and ending date, type 1/1/97 and 1/1/98. Click the command button.
Note that the chart changes to a three-dimensional pie shape.
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 type:
- Open any module in Design view. On the Tools menu, click References, select Microsoft Graph 9.0 Object Library, and then click OK.
- 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
- Switch the form to Form view. When prompted for a beginning and ending date, type 1/1/97 and 1/1/98. Click the command button.
Note the message box that states the graph is not a 2-Dimensional Bar chart.