Microsoft Access VBA Macro Programming

(Tina Sui) #1

PCol = GetPixel(Temp, Temp1, Temp2)
Set ch = Me.Graph0.Object


Pointcount = ch.SeriesCollection( 1 ).Points.Count


For n = 1 To Pointcount
If ch.SeriesCollection( 1 ).Points(n).Interior.Color = PCol Then
MsgBox ch.SeriesCollection( 1 ).Points(n).DataLabel.Text


End If

Next n


End Sub


This code uses theDimstatement to set up objects for the cursor coordinates and the
chart. The API callGetCursorPosis used to find out where the cursor is in terms of pixels.
You may wonder why thexandycoordinates of the Mouse Down event are not used. This is
because they are scaled in twips and we need to use pixels.
The API call is used to get the device context of the current object, which in this case is
our graph. The device context is necessary for the API call to get the pixel color. The variable
PCol is loaded with the pixel color under the cursor by using the API callGetPixel.
The chart objectchpoints to our graph on the form. All the points in the series collection
are then iterated through using a For..Next loop and the interior color is compared to PCol. If
a match is found, then the data label for that segment is displayed in a message box.
Try clicking the pie chart you created earlier. You will find that as you click each segment
the appropriate data label will be displayed.
To take this further, all you then need do is change the query in the chart’s row source to
reflect the data label clicked and to show the detail for it. You can also use a subform below
the chart to show the detail making up that particular segment.


Chapter 34: Drill Down on Charts 327

Free download pdf