Microsoft Office 2000 Developer   

Displaying a Data-bound Chart Control on a PowerPoint Slide

See Also

You can display the most current data by placing a Microsoft Chart control on a Microsoft PowerPoint slide and binding it to an appropriate numeric field in a database.

The following example binds the Chart control to the UnitPrice field of the Products table in the Northwind sample database.

To display a data-bound chart on a PowerPoint slide

  1. On a new slide, draw a Microsoft Chart Control, version 6.0. Draw the control on the slide at an appropriate size, and name the chart control "chtPrices."

  2. Draw a command button control on the form, and name it Bind Chart.

  3. Double-click the command button to display the Visual Basic Editor.

  4. On the Tools menu, click References.

  5. In the References dialog box, select the Microsoft ActiveX Data Objects 2.0 Library. Click OK.

  6. Add the following code to create an ADO Recordset and ADO Connection object. The code also binds the chart to the recordset by setting its DataSource property to the Recordset object.
    Option Explicit
    Private rsProducts As ADODB.Recordset
    Private cn As ADODB.Connection
    
    Private Sub Commandbutton1_Click()
       Set cn = New ADODB.Connection
       ' Change the ConnectionString to one that's appropriate to
       ' the provider and database you are connecting to.
       cn.ConnectionString = "DSN=Northwind"
       cn.Open 
       Set rsProducts = New ADODB.Recordset
       RsProducts.Open "SELECT UnitPrice FROM Products", cn, adOpenStatic, adLockOptimistic
       ' The Chart control is named chtPrices
       Set chtPrices.DataSource = rsProducts
    
       ' Close the Recordset object and the Connection object.
       rsProducts.Close
       cn.Close
    End Sub