OWC10 Chart to print on a report

I am trying to create a Stacked bar chart dynamically in Access VBA to include on a report.  I have to build it with VBA because I need to manipulate the color of the stacked bar slices based on the data values. 

This graph gives me the exact results I need, however, I cannot get the graph to run when the form opens (loads) and cannot get it to function when dropping it onto a report as a subreport.

Create a form called frmChart_5_Floor

Drop a Microsoft Office Chart 10.0 component on the form

Place a button on the form to call the following procedure:

Public Sub BuildPivotChart()

    Dim objPivotChart As OWC10.ChChart
    Dim objChartSpace As OWC10.ChartSpace
    'Dim frm As Access.Form
    Dim strExpression As String
    Dim rs As Recordset
    Dim values1, values2, values3, values4, values5
    Dim axCategoryAxis
    Dim axValueAxis
   
    'Open the form in PivotChart view.
    DoCmd.OpenForm "frmChart_5_Floor", acFormPivotChart
    Set frm = Forms("frmChart_5_Floor")
    Set rs = frm.Recordset
   
    Dim i As Integer
    Dim cnt As Integer
    Dim sValues() As String
    Dim sColors() As String
    cnt = 0

    'VALUES PRE LOADED
    ReDim Preserve sValues(16)
    ReDim Preserve sColors(16)
    sColors(0) = "0,128,128"
    sValues(0) = "0,3,0,1,2"
    sColors(1) = "0,255,255"
    sValues(1) = "23,1,4,0,1"
    sColors(2) = "0,255,0"
    sValues(2) = "0,0,0,1,1"
    sColors(3) = "79,148,205"
    sValues(3) = "10,1,0,0,0"
    sColors(4) = "255,165,0"
    sValues(4) = "0,0,1,0,0"
    sColors(5) = "255,255,0"
    sValues(5) = "22,26,0,13,0"
    sColors(6) = "240,230,140"
    sValues(6) = "0,4,0,1,0"
    sColors(7) = "255,20,147"
    sValues(7) = "0,0,54,1,0"
    sColors(8) = "135,206,250"
    sValues(8) = "0,7,0,6,70"
    sColors(9) = "153,50,204"
    sValues(9) = "0,2,0,0,0"
    sColors(10) = "72,61,139"
    sValues(10) = "0,0,7,6,0"
    sColors(11) = "139,69,19"
    sValues(11) = "3,0,0,0,0"
    sColors(12) = "255,0,255"
    sValues(12) = "0,23,1,0,0"
    sColors(13) = "0,0,255"
    sValues(13) = "1,5,0,3,0"
    sColors(14) = "255,0,0"
    sValues(14) = "0,0,0,22,0"
    sColors(15) = "128,0,0"
    sValues(15) = "23,1,0,7,0"
    cnt = 16
   
    strExpression = "5th" & Chr(9) & "4th" & Chr(9) & "3rd" & Chr(9) & "2nd" & Chr(9) & "1st"
   
    'Clear existing Charts on Form if present and add a new chart to the form.
    'Set object variable equal to the new chart.
    Set objChartSpace = frm.ChartSpace
    objChartSpace.Clear
    objChartSpace.Charts.Add
    Set objPivotChart = objChartSpace.Charts.Item(0)
    objPivotChart.Type = 4 'Stacked Bar
   
    'Set a variable to the Category (X) axis.
    Set axCategoryAxis = objChartSpace.Charts(0).Axes(0)
   
    ' Set a variable to the Value (Y) axis.
    Set axValueAxis = objChartSpace.Charts(0).Axes(1)
   
    ' The following two lines of code enable, and then
    ' set the title for the category axis.
    axCategoryAxis.HasTitle = False
   
    ' The following two lines of code enable, and then
    ' set the title for the value axis.
    axValueAxis.HasTitle = False
   
    'Add Data to the Series.
    'objPivotChart.SeriesCollection(0).SetData chDimCategories, chDataLiteral, strExpression
    Dim s As String
    For i = 0 To cnt - 1
        objPivotChart.SeriesCollection.Add
        If i = 0 Then
            objPivotChart.SeriesCollection(0).SetData chDimCategories, chDataLiteral, strExpression
        End If
        'GetRGB(sColors(i)) is a custom function in a module to return the long color value
        'RGB throws invalid data type (13) error when run in this module - Trust me, it doesn't work!!!!
        objPivotChart.SeriesCollection(i).Interior.Color = GetRGB(sColors(i))
        objPivotChart.SeriesCollection(i).SetData chDimValues, chDataLiteral, sValues(i)
       
        objPivotChart.GapWidth = 10
        objPivotChart.Axes(1).MajorUnit = 5
       
       
        objPivotChart.SeriesCollection(i).DataLabelsCollection.Add
        Dim j As Integer
        For j = 0 To objPivotChart.SeriesCollection(i).Points.Count - 1
            If objPivotChart.SeriesCollection(i).Points(j).GetValue(chDimValues) = 0 Then
                objPivotChart.SeriesCollection(i).DataLabelsCollection(0)(j).Visible = False
            End If
        Next j
    Next i
   
    'Set focus to the form and destroy the form object from memory.
    frm.SetFocus
    Me.SetFocus
    Set frm = Nothing
   
End Sub

Public Function GetRGB(sRGB As String) As Long    'You may have to create a module to place this function in

    Dim r, g, b As Integer
  
    Dim c() As String
   
    c = Split(sRGB, ",")
   
    r = CInt(c(0))
    g = CInt(c(1))
    b = CInt(c(2))
   
    GetRGB = RGB(r, g, b)
   
End Function

When calling it from the button it works fine.  Now add the call to the Form_Load and/or Form_Open events and try closing and opening the form.  I get a "Property Not Found" error. 

I need some suggestions on how to get this to work, and alternatives if it is not possible.

Thanks



Answer this question

OWC10 Chart to print on a report

  • MPL05

    Solved this on my own. Was able to set the objChartSpace Object variable and it worked fine.
  • .Net Learner

    I have a question for you...

    I am creating my pivotchart also using VB. Below is the pivot form load. I have the chDimValues being created using the statement below.

    Private Sub Form_Load()
    Dim c As ChartSpace

    'Set the Chart data.
    Set c = Me.ChartSpace

    c.SetData chDimCategories, chDataBound, strCategory
    c.SetData chDimCategories, chDataBound, strCategory
    c.SetData chDimValues, chDataBound, strData

    c.Charts(0).Axes(0).HasTitle = True
    c.Charts(0).Axes(0).Title.Caption = strCategoryCaption
    c.Charts(0).Axes(1).HasTitle = True
    c.Charts(0).Axes(1).Title.Caption = strValueCaption


    If (strGPSLoss = "all" Or strGPSLoss = "") And (strLossType = "all" Or strLossType = "") And (strLoss = "all" Or strLoss = "") And (strReason = "all" Or strReason = "") Then
    c.SetData chDimFilter, chDataBound, "Line"
    c.SetData chDimSeriesNames, chDataBound, strSeries
    Else
    c.SetData chDimFilter, chDataBound, strSeries
    c.SetData chDimSeriesNames, chDataBound, "line"
    End If
    End Sub

    This give me a sum of all the data that is the same in the series, etc.

    What I need is to get the average. Would you know how to do this programatically. I cannot locate anything in any documentation that shows this.

    Thanks for your help

    Eddi Rae



  • OWC10 Chart to print on a report