Microsoft KB Archive/154581

= ACC: How to Use Automation to Set Graph Trend Lines =

Article ID: 154581

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q154581



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



SUMMARY
This article shows you how to use Automation to add, remove, and set trend lines on 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:

186855 ACC97: Microsoft Access 97 Sample Graphs Available in Download Center



MORE INFORMATION
Different types of trend measurements exist. You can use the Type property to select a specific trend to use in your graph. You can also use the Add method to add trend lines to your graph, or use the Delete method to remove trend lines. Trend lines only apply to two-dimensional graphs of type "Bar," "Column," "Line," and "Scatter." The following examples use a bar graph.

Example - Add a Trendline and set its Type
The following example adds a trend line to the first series of data and sets it to be of type Polynomial:  Open the sample database Northwind.mdb. Create a new form not based on any table or query in Design view. On the Insert menu, click Chart, and then click in the detail section of the form to start the Chart Wizard. In the "Which table or query would you like to use to create your chart?" dialog box, click Queries, and then select the Employee Sales By Country query. Click Next. In the "Which fields contain the data you want for the chart?" dialog box, add the Country and SaleAmount fields to the Fields For Chart box. Click Finish. Your graph will appear in the form. 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.</li> Add a command button to the form with the following properties:

Command button:

Caption: Add Trend Line

OnClick: =AddTrendLine

</li> On the View menu, click Code to view the form's module.</li>  Add the following code to the form's module: Function AddTrendLine Dim GraphObj As Object Set GraphObj = Me![MyGraph].Object.Application.Chart GraphObj.SeriesCollection(1).TrendLines.Add GraphObj.SeriesCollection(1).TrendLines(1).Type = 3 End Function </li> Switch the form to Form view. When prompted for a beginning and ending date, enter 1/1/94 and 1/1/95 respectively. Click the Add Trend Line button. Note the trend line added to the chart.</li></ol>

Example - Remove a Data Series Trendline
The following example removes the trend line from the first series of data being graphed in the earlier example:

<ol> Use the form created in the first example, and add another command button with the following properties:

Command button:

Caption: Remove Trend Line

OnClick: =RemoveTrendLine

</li> On the View menu, click Code to view the form's module.</li>  Add the following code to the form's module: Function RemoveTrendLine Dim GraphObj As Object Set GraphObj = Me![MyGraph].Object.Application.Chart GraphObj.SeriesCollection(1).TrendLines(1).Delete End Function </li> Switch the form to Form view. When prompted for a beginning and ending date, enter 1/1/94 and 1/1/95 respectively.</li> Click the Add Trend Line button to add the trend line to the chart.</li> Click the Remove Trend Line button. Note the trend line is removed from the chart.</li></ol>

<div class="references_section">