Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Use Automation to Modify MS Graph Object


View products that this article applies to.

This article was previously published under Q202147
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

↑ Back to the top


Summary

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.

↑ Back to the top


More information

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

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

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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. In the Database window, click Forms under Objects.
  2. Click the New button on the Database Window toolbar.
  3. Click the Chart Wizard, and then select the Category Sales for 1997 query. Click OK.
  4. Move both fields from the Available Fields list to the Fields For Chart list, and then click Next.
  5. Click Finish.
  6. On the View menu, click Design View.
  7. Right-click the graph, click Properties, and then click the All tab.
  8. Change the Name property to Graph1.
  9. Close the property sheet.
  10. 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 File menu, click Close and Return to Microsoft Access.
  8. 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 File menu, click Close and Return to Microsoft Access.
  6. 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.

↑ Back to the top


References

For more information about Automation, click Microsoft Access Help on the Help menu, type understanding automation in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB202147, kbhowto

↑ Back to the top

Article Info
Article ID : 202147
Revision : 2
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 292