Visual Basic Concepts
The Microsoft Chart control is a data bound control, and allows you to graphically represent numeric data. Unlike other data bound controls, however, the Chart control cannot be used with the Remote Data Control, or the Data control. It can be used with the ADO Data Control, an ADO Recordset, and the Data Environment. This example will show how to open an ADO Recordset that contains the fields you want to display, and set the Chart control's DataSource property to the Recordset object. If the first field contains string data, that data will be used as the X axis labels.
The example below shows three series of data by first creating a Recordset object that has four fields; the first field contains the labels for the X axis, and remaining fields are displayed as series data.
Option Explicit
' Be sure to set a reference to the Microsoft ActiveX Data
' Objects 2.0 Library.
Private rsProducts As New ADODB.Recordset
Private cn As New ADODB.Connection
Private Sub Form_Load()
Dim strQuery As String ' SQL query string.
' First change the path to a valid path for your machine.
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & _
"C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb" ' <-Change this path.
' Open the connection.
cn.Open
' Create a query that retrieves only four fields.
strQuery = "SELECT ProductName, UnitPrice, " & _
"UnitsInStock, UnitsOnOrder FROM Products WHERE SupplierID = 1"
' Open the recordset.
rsProducts.Open strQuery, cn, adOpenKeyset
' Set the DataSource to the recordset.
With MSChart1
.ShowLegend = True
Set .DataSource = rsProducts
End With
End Sub