ACC: How to Change a Graph Chart Type Using OLE Automation

Last reviewed: January 6, 1998
Article ID: Q154582
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

This article demonstrates 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.

MORE INFORMATION

It is possible to change the chart type of a Microsoft Graph using Automation. 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 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: Determine 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
Keywords          : kbinterop kbtool IntpGrph
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 6, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.