ACC: How to Use Automation to Modify MS Graph Object (95/97)

ID: Q186721


The information in this article applies to:


SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes how to use Automation to add or remove a title of a graph and how to hide or show a legend for a graph.

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:

Q186855 ACC97: Microsoft Access 97 Sample Graphs Available on MSL


MORE INFORMATION

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the file, or perform these steps on a copy of the Northwind database.

NOTE: To use the Visual Basic for Applications procedures in this article, you must have a reference to Microsoft Graph.

Creating a Sample Graph

To be able to use the procedures to add or to remove a title or to hide or to show a legend, first create the following form:
  1. On the Forms tab, click New.


  2. Click the Chart Wizard, and then select the Category Sales for 1994 query in Microsoft Access 95 or Category Sales for 1995 query in Microsoft Access 97. Click OK.


  3. Move both fields from the Available Fields list to the Fields For Chart list, and then Click Next.


  4. Click Finish.


  5. On the View menu, click Design View.


  6. Right-click on the graph, click Properties, and then click the All tab.


  7. Change the Name property to Graph1.


  8. Close the property sheet.


  9. Save the form with the name GraphTest, and then close it.


Adding or Removing a Graph's Title

The following example demonstrates how to use Automation to add or remove a graph's title.
  1. Open the GraphTest form in Design view.


  2. On the View menu, click Properties, and then click the All tab.


  3. Change the DataEntry property of the form to Yes.


  4. Add the following command button to the form:
    
          Command Button
          --------------
          Name: Title
          Caption: Toggle Caption 


  5. Set the OnClick property of the Toggle Caption command button to the following event procedure:
    
          Sub Title_Click()
    
             '-------------------------------------------------------
             ' Show or hide the title by using the Not Operator to
             ' reverse the state of the HasTitle property
             '-------------------------------------------------------
    
             Me!Graph1.Hastitle = Not Me!Graph1.Hastitle
    
             ' If the graph has a title, set the Caption to something
             If Me!Graph1.Hastitle Then
                Me!Graph1.ChartTitle.Text = "Sample Title"
             End If
    
          End Sub 


  6. Add the following line to the Declarations section of the Sales By Product form's form module if it's not already there:
    
          Option Explicit 


  7. On the View menu, click Form view, and then click the Toggle Caption button. Note that the graph's title either appears or disappears.


Hiding or Showing a Graph's Legend

The following example demonstrates how to hide or to show a legend on a graph.
  1. Open the GraphTest form in Design view.


  2. Add a command button with the following properties to the form:
    
          Command Button
          ----------------------
          Name: Legend
          Caption: Toggle Legend 


  3. Set the OnClick property of the Toggle Legend command button to the following event procedure:
    
          Sub Legend_Click ()
    
             '-----------------------------------------------------------
             ' If the legend is not present, show it; otherwise, hide it.
             ' Use the Not Operator to reverse the state of the HasLegend
             ' property of the graph.
             '-----------------------------------------------------------
    
             Me!Graph1.Haslegend = Not Me!Graph1.Haslegend
    
          End Sub 


  4. Add the following line to the Declarations section of the Sales By Product form's form module if it's not already there:
    
          Option Explicit 


  5. On the View menu, click Form view, and then click the Toggle Legend button. Note that the graph's legend either appears or disappears.


NOTE: Depending on the how the graph was created, you may have to click the Toggle Legend button twice the first time that you use it to see the described functionality.


REFERENCES

For more information about OLE Automation, search for "OLE Automation," and then "Interoperability with Microsoft Word and Microsoft Excel (Common Questions," using the Microsoft Access Help menu.

For further information about manipulating a chart with Visual Basic for Applications code, see the file Vba8grp.hlp. This file is located in the Morehelp subfolder of the Valupack folder on your Microsoft Office 97 Professional Edition compact disc or on your Microsoft Access 97 compact disc.

You may also want to see the following article in the Microsoft Knowledge Base:

Q128364 Microsoft Graph VBA Help File Available on MSL

Additional query words: link embed


Keywords          : kbdta kbgraph 
Version           : WINDOWS:7.0,97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 19, 1999