Microsoft KB Archive/119469

= ACC2: How to Use OLE Automation to Modify MS Graph Object =

Article ID: 119469

Article Last Modified on 11/6/2000

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q119469



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

This article describes how to use OLE Automation to modify a Microsoft Graph version 5.0 graph's title and hide or show the graph's legend.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Building Applications" manual.



Changing a Graph's Title
The following example demonstrates how to use OLE Automation to modify a Graph 5.0 graph's title.

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

 Open the sample database NWIND.MDB. Open the Sales By Product form in Design view. Set the form's DefaultEditing property to Allow Edits.  Add the following controls to the form:

     Text Box -     Name: MyTitle

Command Button -     Name: Title Caption: Show Caption   Set the Show Caption command button's OnClick property to the following event procedure.

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

Sub Title_Click Dim The_title As String

'--     ' This function reads the text box. If it is null or blank, it     ' supplies a generic title, otherwise it adds a title and sets ' the caption to the text box's contents. '--

If IsNull(Me![MyTitle]) Or Me![MyTitle] = "" Then The_title = "No Title" Else The_title = Me![MyTitle] End If

If Me![Title].Caption = "Show Title" Then Me![Embedded13].Object.Application.Chart.Hastitle = -1

Me![Embedded13].Object.Application.Chart.Charttitle.Caption = _ The_title Me![Title].Caption = "Hide Title" Else Me![Embedded13].Object.Application.Chart.Hastitle = 0 Me![Title].Caption = "Show Title" End If

End Sub   Add the following line to the Declarations section of the Sales By Product form's form module:

<pre class="fixed_text">     Option Explicit </li> View the form in Form view. Type any text in the text box you created in step 4, and then click the Show Caption button. The graph's title will change to the text box's text.</li></ol>

Hiding or Showing a Graph's Legend
The following example demonstrates how to hide or show a Graph 5.0 graph's legend.

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

<ol> Open the sample database NWIND.MDB.</li> Open the Sales By Product form in Design view.</li>  Add a command button with the following properties to the form:

<pre class="fixed_text">     Name: Legend Caption: Show Legend </li>  Set the Show Legend command button's OnClick property to the following event procedure:

Sub Legend_Click

'--     ' If the legend is not present, show it, otherwise hide it. '--

If Me![Legend].Caption = "Show Legend" Then Me![Embedded13].Object.Application.Chart.Haslegend = -1 Me![Legend].Caption = "Hide Legend " Else Me![Embedded13].Object.Application.Chart.Haslegend = 0 Me![Legend].Caption = "Show Legend" End If     End Sub </li>  Add the following line to the Declarations section of the Sales By Product form's form module:

<pre class="fixed_text">     Option Explicit </li> View the form in Form view. Click the Show Legend button to toggle between hiding and showing the form's legend.</li></ol>

<div class="references_section">