|
by Garry Robinson from GR-FX Pty Limited For a long time my approach to managing graph objects was to set them up with the required look and then change the graph by manipulating the query. When Access 97 became the standard version used by most of my clients and shareware users, it became time to invest some resources into manipulating the complicated Graph object. These lessons concentrate on the techniques that you can use to program the object plus demonstrates some property changes that you can utilize to make graph more interactive. Technology Introduction Microsoft Access ships with many smart components including forms and reports, text and combo and list boxes, options groups and the ability to add Active X and OLE components. The most complicated of the standard objects is Microsoft Graph. As MS Graph is part of the standard install process with Access, you will generally find that the object is available on most computers that Access is installed on. The reason that the graph object is complicated is probably because it has so many features and also because the changing of any of these features usually involves a corresponding change to some other feature to make the object act in a satisfactory manner. The other reason is that whilst the graph wizards would infer that the graph object is a fully integrated component like a text box, it really has to be managed in code in the same way that you might manipulate Excel from Access.
The demonstration database is called smartgraph.mdb. Inside that database is a table called zWorld_Demo plus a form called z_GraphPlus with the Visual Basic that manipulates MS Graph. Listing 1 shows the demonstration data used in these lessons. You can find all the code used in these lessons plus the Access forms when you purchase "The Toolshed".
Listing 1 A number of lines of information from the zWorld_Demo table Adding A Smart Graph To Your Database (With Some VB) The smartest way to make the most of these lessons is to simply import the form (see figure 2) from the demonstration database into your application, change the row source of the graph object to suit your data and then deploy the form. The code under the form will work for any data in any Access 97 or 2000 database.
Changing The Microsoft Access Graph Type (Lesson 2) by Garry Robinson from GR-FX Pty Limited To change the graph type from a bar graph to a pie graph, using the option box, I use the following code behind the form Private Sub GrafType_AfterUpdate() [GraphFX].Object.Application.Chart.ChartType = GrafType End Sub Listing 3 Changing the Graph Types GraphType is the name of the option box and [GraphFX] is the name of the MS Graph Object. The trick here is that each of the option boxes has a option value property that matches the correct constant that relates to the ChartType. These constants are shown in table 4 as follows
Table 4 - The constants used used in MS Graph to change the graph type Yes I know I should never store the constants as hard coded numbers but the resultant code is simpler as the default value will only allow an integer value.
Turning the Microsoft Access Graph Legend On and Off (Lesson 3) by Garry Robinson from GR-FX Pty Limited Adding the legend to the graph starts to demonstrate the vagaries of the Graph object (see listing 5). Initially you would imagine that it would simply be a case of turning the Legend on and off and yes you can do this. But in this case turning the legend on causes the graph width to be reduced in size to accommodate the space that is automatically allocated to the legend. This doesnt matter if the form that you are using occupies a full SVGA screen but when it occupies a smaller amount of space, the auto spacing allocation may not be so useful. After a lot of experimentation, my preferred setting is to use the Legend as a pseudo title line and display it at the top of the screen. This means I do not have to tangle with the same issues with title as well as legends and it also saves some valuable real estate for the graph object. Private Sub legendTgl_Click() ' Turn the legend on at the top of
the object ' Use xlLegendPositionTop in place
of -4160 With [GraphFX].Object.Application.Chart If legendTgl = 0 Then Else End With End Sub Listing 5 VBA that will turn the Legend On And Off The most important lessons demonstrated here is the fact that if you alter the property of the graph is some way, it is fairly likely that other parts of the graph are going to be affected. If this is an issue, you are going to have to work out a way to fix those other properties with some compensating property changes. This is one of the reasons that manipulating the graph object can be time consuming. The other lesson is to use the With Statement whenever you can when programming with the graph object. This will make for cleaner looking code and it will also improve performance as the reference to the object only has to be established once rather than for each line of code.
Viewing the Data Table in Microsoft Access Graph (Lesson 4) Graph 8 allows you to view the data that makes up the graph by turning on the data table. This is handled in Listing 6 by referencing the Boolean constant provided by the dataTableTgl toggle button. Private Sub dataTableTgl_Click() With Me![GraphFX].Object.Application.Chart .HasDataTable = dataTableTgl End With End Sub Listing 6 Toggling the Data Table View On and Off
If you like these lessons, why not try Graf-FX for some very versatile Access graphing.
Related Documents at VB123 Published 1999-08 |
|
Links >>> Home | Search | Workbench | Orders | Newsletter | Access Security | Access professionals |