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

ID: Q154582

The information in this article applies to:

SUMMARY

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

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:

   ARTICLE-ID: Q186855
   TITLE     : ACC97: Microsoft Access 97 Sample Graphs Available on MSL

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:

 1. Open the sample database Northwind.mdb.

 2. Create a new form not based on any table or query in Design view.

 3. On the Insert menu, click Chart. Click and drag a chart to the form.

 4. In the first dialog box of the Chart Wizard, under View, click Queries,
    and then select the "Employee Sales By Country" query. Click Next.

 5. 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.

 6. 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.

 7. Add a command button to the form with the following properties:

       Command button: MyButton
          Name: MyButton
          Caption: Pie Chart
          OnClick: [Event Procedure]

 8. On the View menu, click Code to view the form's module.

 9. 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).

10. 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".

11. Switch the form to Form view. When prompted for a beginning and ending
    date, enter "1/1/94" and "1/1/95" respectively (without the quotation
    marks). Click the command button. Note that the chart changes to a
    3-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:

1. 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.

2. 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

3. Switch the form to Form view. When prompted for a beginning and ending
   date, enter "1/1/94" and "1/1/95" respectively (without the quotation
   marks). Click the command button. Note the message box that states the
   graph is not a 2-Dimensional bar chart.

REFERENCES

For more information about the Type property and for a listing of chart types and their constants, open the Microsoft Graph help file (you must have the Vbagrp8.hlp help file installed), and search on "Type" and then view the available topics.

For more information about Automation, search on "Automation," and "Automation with Microsoft Access" using the Microsoft Access 97 Help Index.

For more examples of Automation between Microsoft Access and Microsoft Graph, see the Office 97 Automation Help File, Auto97.exe. The Auto97.exe file contains the Microsoft Office 97 Automation Help file created by Microsoft Technical Support. This Help file contains Automation theory and multiple examples on automating all the Office 97 products (Microsoft Access, Microsoft Excel, Microsoft Word, Microsoft PowerPoint, and Microsoft Outlook) as well as Microsoft Graph, DAO/ODBCDirect, Microsoft Binder, OLE Messaging, and the Office Assistant.

For more information about how to obtain this help file, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q167223
   TITLE     : Microsoft Office 97 Automation Help File Available on MSL

Additional query words:
Keywords          : kbinterop kbtool IntpGrph 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998