I wrote an article earlier about ActiveX control (
FDGrid) to allow
FlexGrid control as a
DataGrid control. To see this ActiveX and its application, click here.
Now I create a new AciveX control (
MKGraph) to transfer data from
FlexGrid to a graph chart:
- Connect with database
FlexGrid with table
- Add or Edit records
- Choose numeric columns
- Choose graph type
- Edit title and legend
- Click to graph
- Save graph chart
- Copy graph chart
When using my ActiveX control (
MKGraph), you must load the grid with data from the database file, see the code in the form (
frmMain). Some properties and methods of my ActiveX are in the following table:
|Method/Property ||Definition ||Example |
|Set database name|
MKGraph1.DatabaseName = FileName
|Set the |
Recordset to grid
MKGraph1.DataSource = rs
|Add new record to grid|
|Save record after add|
|Move to first record|
|Move to last record|
|Move to next record|
|Move to previous record|
|Tell ActiveX which columns to draw|
|Begin to graph (three steps)|
To use my ActiveX (
MKGraph), you must add only one file from files: "Microsoft DAO 3.51 (or 3.6) Object Library" to the reference and add (
MKGraph) control to the Toolbox.
Using the Code
Dim MyDataFile As String
Dim MyDb As Database
Dim MySql As String
MyDataFile = App.Path + "\DataFile\" + "Sale.mdb"
Set MyDb = OpenDatabase(MyDataFile, False, False, ";pwd=" & "")
MySql = "SELECT * FROM Products Order by ProductID"
MKGraph1.DatabaseName = MyDataFile
MKGraph1.RecordSource = MySql
MKGraph1.RecordsetType = vbRSTypeDynaset
When you click
cmdDraw button, the ActiveX control will graph with three steps:
- Choose the graph type (when you see the form which has the caption "Graph type": click any graph type).
- Edit the title and legend of graph (when you see the form which has the caption "Graph data").
- Graph chart (now you see your graph on the final form).
When extracting the file FlexToGraph.zip, you can find the file MKGraph.ocx in the folder ActiveXcontrol.
Find database file Sale.mdb in the DataFile folder.
Find the project
prjFlexGraph (to test the ActiveX control) in the folder FlexToGraph.
This project has only one form (
This form has:
- Button to load data from database file
- Button to graph
- Button to add record
- Button to delete record
- Button to update (save record)
- Button to cancel update
- Four buttons for
- Button to exit
I hope this article is useful and helps you to graph some data in your applications. Please tell me if you have any ideas or if you find any problems. Thanks to CodeProject and thanks to all.
- 30th May, 2008: Initial post