How to Programmatically Build a PivotChart View in an Access Form

This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills. 

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp). 

In Microsoft Access 2002, you can view tables, queries, views, functions, and forms in PivotChart view. This is possible because Access 2002 natively hosts the Microsoft Office Web Components. This article shows you how to use the Office Web Components object model to programmatically build a PivotChart view in a Microsoft Access form.
Microsoft Access exposes the  object model through the  property of a form. There are a number of objects and methods that you must use to programmatically build a PivotChart view in a Microsoft Access form. The following table briefly explains what each method does.

Name Description
 object Represents one chart in the chart workspace.
 method Creates a new, empty chart or adds a series to a chart, depending on the proceeding object.
 method Deletes every chart in the chart workspace and resets all the formatting to the default values.
property Returns the  collection for the specified chart.
Sets data for the specified chart object.

: 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.

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.
  1. Start Access.
  2. On the  menu, point to , and then click .
  3. Create a new query that is based on the Employees and the Orders tables.
  4. Add the following fields to the query:
       Query: qrySales
       Type: Select Query
       Join: Employees.EmployeeID <-> Orders.EmployeeID
       Field: LastName
       Table: Employees
       Total: Group By
       Sort:  Ascending
       Field: OrderID
       Table: Orders
       Total: Count
  5. Save the query as , and then close it.
  6. Create a new form that is based on the qryOrdersbyEmployees query, and then open it in Design view.
  7. Add all the fields to the form.
  8. Save the form as , and then close it.
  9. On the menu, click . This opens the Visual Basic Editor and inserts a new, blank module.
  10. On the  menu, click .
  11. Click .
  12. Browse to and select the following file, and then click . This adds a reference to the Microsoft Office XP Web Components library.
    C:\Program Files\Common Files\Microsoft Shared\Web Components\10\OWC10.DLL
  13. Click  to close the  dialog box.
  14. Add the following Visual Basic for Applications (VBA) code to the new module:
      Sub BuildPivotChart()
      Dim objPivotChart As OWC10.ChChart
      Dim objChartSpace As OWC10.ChartSpace
      Dim frm As Access.Form
      Dim strExpression As String
      Dim rs As Recordset
      Dim values
      Dim axCategoryAxis
      Dim axValueAxis
      'Open the form in PivotChart view.
      DoCmd.OpenForm "frmPivotChart", acFormPivotChart
      Set frm = Forms("frmPivotChart")
      Set rs = frm.Recordset
      'Loop through Recordset to obtain data for the chart and put in strings.
        Do While Not rs.EOF
            strExpression = strExpression & rs.Fields(0).Value & Chr(9)
            values = values & rs.Fields(1).Value & Chr(9)
      Set rs = Nothing
      'Trim any extra tabs from string.
      strExpression = Left(strExpression, Len(strExpression) - 1)
      values = Left(values, Len(values) - 1)
      'Clear existing Charts on Form if present and add a new chart to the form.
      'Set object variable equal to the new chart.
      Set objChartSpace = frm.ChartSpace
      Set objPivotChart = objChartSpace.Charts.Item(0)
      'Set a variable to the Category (X) axis.
      Set axCategoryAxis = objChartSpace.Charts(0).Axes(0)
      ' Set a variable to the Value (Y) axis.
      Set axValueAxis = objChartSpace.Charts(0).Axes(1)
      ' The following two lines of code enable, and then
      ' set the title for the category axis.
      axCategoryAxis.HasTitle = True
      axCategoryAxis.Title.Caption = "Employees"
      ' The following two lines of code enable, and then
      ' set the title for the value axis.
      axValueAxis.HasTitle = True
      axValueAxis.Title.Caption = "Orders"
      'Add Series to Chart and set the caption.
      objPivotChart.SeriesCollection(0).Caption = "Orders"
      'Add Data to the Series.
      objPivotChart.SeriesCollection(0).SetData chDimCategories, chDataLiteral, strExpression
      objPivotChart.SeriesCollection(0).SetData chDimValues, chDataLiteral, values
      'Set focus to the form and destroy the form object from memory.
      Set frm = Nothing
    End Sub
  15. On the menu, click .
  16. Type the following into the Immediate window, and then press ENTER:
  17. On the menu, click .
For more information about the  object model, see the "Microsoft Office Web Components Visual Basic Reference" Help file (Owcvba10.chm). You can find this Help file in the language-specific folder (for example, the 1033 folder) at the following default location:
C:\Program Files\Common Files\Microsoft Shared\Web Components\10
For example, if you are using the English version of Microsoft Office XP, you can find this Help file in the following location:
C:\Program Files\Common Files\Microsoft Shared\Web Components\10\1033