Getting Data Into
Your Graph Object Lesson
3Now we come to the section where we
need to add our own data in a programmatic fashion to the object. I thought this was
going to be an easy one but No No No. The usual wild search through MSDN etc and
finally I came up with something that showed me how to add data.
For this example, add a new button to the graph which I have
called cmdAddData

Now add the following code to the Form_Load event.
Private Sub Form_Load()
' Set the standard format for the graph
' Search CF_Text for more info on formats supportedWith OLE1
.Format = "CF_TEXT"
.SizeMode = vbOLESizeStretch
.CreateEmbed "", "MSGRAPH"
End With |
Now you are going to build a string that allows you to transfer the data to the
object. How you get this data in the first place is not part of this lesson but the datatext
method initiates the transfer.
Private Sub
cmdAddData_Click()
' Add some data to the graph
Dim msg, NewLine, Tabb
Tabb = vbTab
NewLine = vbNewLine
msg = Tabb + "Beer" & Tabb & "Wine" & Tabb &
"Spirits"
msg = msg & NewLine + "Australia" & Tabb & 100 & Tabb
& 65 & Tabb & 14
msg = msg & NewLine + "USA" & Tabb & 120 & Tabb & 85
& Tabb & 33
msg = msg & NewLine + "UK" & Tabb & 130 & Tabb & 53
& Tabb & 24
With OLE1
' Use this if you want the object to remain
hidden until you click on the data button
' .DoVerb vbOLEShow
If .AppIsRunning Then
.DataText = msg
.Update
Else
MsgBox "Graph is not active"
End If
End With
End Sub |
Other options worth looking into would be using arrays, the flex-grid control
and automating transfers from text files.
Switching The Standard Data
Format
As it is highly likely that you will be transferring
your data from a recordset, you will need to change the project setup from
the standard Series in rows to Series
in Columns. To do this, Edit
the graph object and select the Data menu as
in the screen shot below.

Click here for the
download file if you own "The Toolshed" Else ??? |