Access VBA Chart Problem

TimW

Registered User.
Local time
Today, 17:54
Joined
Feb 6, 2007
Messages
90
I am having problems with the following code:
Code:
Private Sub Form_Load()
    Dim graphOb As Object
 
    Dim rsCount, i, j As Integer
 
    Dim mySQL As String
    Dim db As Database
    Dim rs As DAO.Recordset
    Dim myData, arrA, arrB, arrC, arrD As Variant
 
    Dim ns As Object
 
    Set db = CurrentDb
 
    '   ************************************* _
        *   Bring in information from Query * _
        *************************************
        mySQL = "SELECT * " & _
                "FROM qryDowntime4Graph " & _
                "WHERE ([ShiftLogRef] = " & Forms!frmShiftLog.Reference & ")" & _
                " ORDER BY [DowntimeStartDate];"
        'Debug.Print mySQL
 
    '   **************************************** _
        *   Create recordset and put into array * _
        *   ~ myData ~                          * _
        ****************************************
    Set rs = db.OpenRecordset(mySQL, dbOpenDynaset)
    With rs
        rsCount = .RecordCount
        If .RecordCount > 0 Then
            .MoveFirst
            ReDim myData(rsCount, 8) ' 8 is the number of columns in qryDowntime4Graph
            ReDim arrA(rsCount)
            ReDim arrB(rsCount)
            ReDim arrC(rsCount)
            ReDim arrD(rsCount)
            myData = .GetRows(rsCount)
        Else:
            MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"
            Exit Sub
         End If
    End With
    ' Finished with rs
    rs.Close
    Set rs = Nothing
    For i = 0 To rsCount - 1
        arrA(i) = myData(2, i)  '   Start DT information
        arrB(i) = myData(4, i)  '   Cause
        arrC(i) = myData(5, i)  '   Duration
        arrD(i) = myData(1, i)  '   Stopped
 
    Next
 
 
    Set graphOb = Me.chartDTGantt
 
'    Me.graphSample.RowSource = "tblData"
    ' *** define graph  ***
    With graphOb
        .ChartType = xlBarStacked
        .HasTitle = True
        .ChartTitle.Text = "Downtime"
            With .Axes(xlCategory)
                .HasTitle = True
                .AxisTitle.Caption = "Downtime reason"
            End With
            With .Axes(xlValue)
                .HasTitle = True
                .AxisTitle.Caption = "Date - shift"
                .MinimumScale = #9/7/2009#
                .MaximumScale = #9/9/2009#
 
               .TickLabels.NumberFormat = "h:mm AM/PM"
            Debug.Print graphOb.SeriesCollection.Count
 
            End With
            ' ***  Delete series collection before adding new   ***
            Do Until .SeriesCollection.Count = 0
                .SeriesCollection(1).Delete
            Loop
            '   ***************************************************** _
                *   Create two series collections   * _
                *   1, For start time &     * _
                *   2, For duration     * _
                *****************************************************
 
           With .SeriesCollection.NewSeries ' [COLOR=red]code stops here[/COLOR]
                .Values = arrA
                .XValues = arrB
            End With
            With .SeriesCollection.NewSeries
                .Values = arrC
                .Name = "Downtime"
            End With
 
    End With
 
 
 
End Sub
I get a runtime error 438. object does not support this property or method.
When i type the line in, the code automatically capitalises the line. So something is recognised. If i change the line to With .SeriesCollection.Add this done the same.
Do i need to reference anything else? The .count and .delete methods are working so why not the .newseries

Has anyone got any light to shed on this please?

Thanks

TimW
 
I would suggest first getting it all to work without using all of the different WITH code. Use the explicit reference until it works and then go back to modify it. It looks a bit like you might have ended up on a different level than you were thinking you were.
 
Further to my last post. I have changed tack and i have the following code working using Microsoft office Web components. I created a blank form which defaults to a pivotchart view. I had some problems running the code but when linking to a new blank form these problems went away!
Code:
Sub create_graph()
 
   Dim graphOb As Object
   Dim rsCount, i, j As Integer
   Dim mySQL As String
   Dim db As Database
   Dim rs As DAO.Recordset
   Dim myData, arrA, arrB, arrC, arrD As Variant
   Dim ns As Object
   Dim axCategoryAxis
   Dim axValueAxis
   Dim ochart
   Dim oSeries1, oSeries2
   
   Dim startDate As Date
   Dim endDate As Date
    
    Set db = CurrentDb
    
    '   ************************************* _
        *   Bring in information from Query * _
        *************************************
        mySQL = "SELECT * " & _
                "FROM qryDowntime4Graph " & _
                "WHERE ([ShiftLogRef] = " & Forms!frmShiftLog.Reference & ")" & _
                " ORDER BY [DowntimeStartDate];"
        'Debug.Print mySQL
        
    '   **************************************** _
        *   Create recordset and put into array * _
        *   ~ myData ~                          * _
        ****************************************
    Set rs = db.OpenRecordset(mySQL, dbOpenDynaset)
    With rs
        rsCount = .RecordCount
        If .RecordCount > 0 Then
            .MoveFirst
            ReDim myData(rsCount, 8) ' 8 is the number of columns in qryDowntime4Graph
            ReDim arrA(rsCount)
            ReDim arrB(rsCount)
            ReDim arrC(rsCount)
            ReDim arrD(rsCount)
            myData = .GetRows(rsCount)
        Else:
            MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"
            Exit Sub
         End If
    End With
    ' Finished with rs
    rs.Close
    Set rs = Nothing
    
    If IsNull(myData(2, 0)) Then
        MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"
        Exit Sub
    End If
    
    For i = 0 To rsCount - 1
        arrA(i) = myData(2, i)  '   Start DT information
        arrB(i) = myData(4, i)  '   Cause
        arrC(i) = myData(5, i)  '   Duration
        arrD(i) = myData(1, i)  '   Stopped
    Next
    '   ******************************** _
        *   Open Graph form
    If Not CurrentProject.AllForms("frmnewChart").IsLoaded Then
        DoCmd.OpenForm "frmnewChart", acFormPivotChart
    End If
    
 ' ******************************************************************* _
 *      Start of graph stuff                                         * _
 *********************************************************************
 
Select Case Forms!frmShiftLog!shift
    Case "Days"
        startDate = Forms!frmShiftLog!ShiftDate.Value + 0.33333
        endDate = startDate + 0.375
    Case "Nights"
        startDate = Forms!frmShiftLog!ShiftDate.Value + 0.70833
        endDate = startDate + 0.4
    Case "Dawn"
        startDate = Forms!frmShiftLog!ShiftDate.Value + 0.125
        endDate = startDate + 0.208334
 End Select

 
    ' *** define graph  ***
      Set ochart = Forms("frmNewChart").ChartSpace.Charts(0)
      Set axCategoryAxis = Forms("frmNewchart").ChartSpace.Charts(0).Axes(0)
      Set axValueAxis = Forms("frmnewChart").ChartSpace.Charts(0).Axes(1)
    
    With ochart
        .Type = chChartTypeBarStacked
        .HasTitle = True
        .Title.Caption = "Downtime"
    End With
    With axValueAxis
                .HasTitle = True
                .Title.Caption = "Time"
                .Scaling.Minimum = startDate
                .Scaling.Maximum = endDate
                .NumberFormat = "h:mm AM/PM"
                .MajorUnit = 0.0416666 ' 1 hour
                
                
    End With
    With axCategoryAxis
                .HasTitle = True
                .Title.Caption = "Downtime Cause"
    End With
      
'     ***  Delete series collection before adding new   ***
        With ochart
            i = .SeriesCollection.Count - 1
            For j = i To 0 Step -1
                .SeriesCollection.Delete (j)
            Next
            
       
            '   ***************************************************** _
                *   Create two series collections   * _
                *   1, For start time &     * _
                *   2, For duration     * _
                *****************************************************
            .SeriesCollection.Add
            .SeriesCollection.Add
            .SeriesCollection(0).SetData chDimValues, chDataLiteral, arrA
            .SeriesCollection(0).SetData chDimCategories, chDataLiteral, arrB
            .SeriesCollection(1).SetData chDimValues, chDataLiteral, arrC
      
            For i = 0 To rsCount - 1
                If arrD(i) = "true" Then
                    .SeriesCollection(1).Points(i).Interior.Color = "Red"
                Else
                    .SeriesCollection(1).Points(i).Interior.Color = "Green"
                    
                End If
            Next
            Set oSeries1 = .SeriesCollection(0)
            Set oSeries2 = .SeriesCollection(1)
            .PlotArea.Interior.Color = RGB(230, 237, 215)
            
   
    End With
    With oSeries1
        .Interior.Color = chColorNone
        .Border.Color = chColorNone
        .Caption = ""
    End With
    With oSeries2
        .Caption = "Downtime"
    End With
     
   

End Sub

However it is not perfect and still allows a user to play around with settings - or perhaps the users Access is set up differently and the parts of the graph i have not defined can still be altered. I will try to get this bullet proof, but if anyone has any tips, I would be forever in their debt.

I have found the help files OWCDCH11.CHM but find them very hard to work through.
WHEN i get this working, I will post the code with better notes to help anyone else out there who is having the same amount of fun as i have!:)
 
I've been scouring the internet looking for code to customize some new graphs that I have to make. I'm running into trouble with the ".SeriesCollection(x)" and not finding anyone with any answers. I like that even the Access bibles don't have any of this stuff in them.

It's been a few years since this post. Did you ever get it perfected? Interested in posting an update?? =))
 
Hi Sherry
I have posted my final code for this below. To be honest I have stopped using Microsoft web components for my graphs because everytime I updated my database half the computers that used it lost its reference to the OWC file. (Even though I had adapted some code to stop this, frustraiting!) I currently use the inbuilt graph object in access 2007

You dont say what you are trying to do with .seriescollection?

Tim

Code:
 Sub create_graph_OEE_Daily()
   Dim graphOb As Object
   Dim rsCount, i, j, h, RA As Integer
   Dim mySQL, myCell As String
   Dim db As Database
   Dim rs As DAO.Recordset
   Dim myData, arrA, arrB, arrC, arrD, arrE, arrF As Variant
   Dim ns As Object
   Dim axCategoryAxis
   Dim axValueAxis
   Dim oChart
   Dim oSeries1, oSeries2, oSeries3, oSeries4, oSeries0
   Dim currentDate As Date
   Dim MA As Double
 
   RA = 7 'Rolling average period in days
    If CurrentProject.AllForms("frmShiftLog").IsLoaded Then
        currentDate = Forms![frmShiftLog]!ShiftDate.Value
    End If
    'Debug.Print "Current Date : ", currentDate
    Set db = CurrentDb
    '   ************************************* _
        *   Bring in information from Query * _
        *************************************
        mySQL = "SELECT * " & _
                "FROM [OEE_Daily] " & _
                " ORDER BY [ShiftDate];"
   '     Debug.Print mySQL
    '   **************************************** _
        *   Create recordset and put into array * _
        *   ~ myData ~                          * _
        ****************************************
    Set rs = db.OpenRecordset(mySQL, dbOpenDynaset)
    With rs
        rsCount = .RecordCount
        If .RecordCount > 0 Then
            .MoveFirst
            ReDim myData(rsCount, 6) ' number of columns in table
            ReDim arrA(rsCount)
            ReDim arrB(rsCount)
            ReDim arrC(rsCount)
            ReDim arrD(rsCount)
            ReDim arrE(rsCount)
            ReDim arrF(rsCount)
            myData = .GetRows(rsCount)
        Else:
            MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"
            Exit Sub
         End If
    End With
    ' Finished with rs
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
    If IsNull(myData(2, 0)) Then
        MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"
        Exit Sub
    End If
    For i = 0 To rsCount - 1
        arrA(i) = myData(0, i) '   Date
        If arrA(i) = currentDate Then
            'MsgBox "Current date: " & currentDate & " Point number: " & i
            h = i ' Point number on graph that has selected date
        End If
 '       Debug.Print arrA(i)
        arrB(i) = myData(1, i)  '   Availablity
        arrC(i) = myData(2, i)  '   Performance
        arrD(i) = myData(3, i)  '   Quality
        arrE(i) = myData(4, i)  '   OEE
  '     ********************************* _
        *   Work out rolling average    * _
        *********************************
  '      arrF(i) = myData(5, i)  '
        MA = 0 ' zero moving average
        If i <= RA - 1 Then             ' Work out rolling average if not enough records
            For Z = 0 To i
                MA = MA + myData(4, Z)
            Next
            arrF(i) = MA / (i + 1)
        Else                            '   Rolling average
            For Z = 0 To RA - 1
                MA = MA + myData(4, i - Z)
            Next
            arrF(i) = MA / RA
        End If
 
    Next
    myCell = myData(5, 0)
    '   ******************************** _
        *   Open Graph form
    If Not CurrentProject.AllForms("frmChartOEEDaily").IsLoaded Then
        DoCmd.OpenForm "frmChartOEEDaily", acFormPivotChart
    End If
 ' ******************************************************************* _
 *      Start of graph stuff                                         * _
 *********************************************************************
'
    ' *** define graph  ***
      Set oChart = Forms("frmChartOEEDaily").ChartSpace.Charts(0)
      Set axCategoryAxis = Forms("frmChartOEEDaily").ChartSpace.Charts(0).Axes(0)
      Set axValueAxis = Forms("frmChartOEEDaily").ChartSpace.Charts(0).Axes(1)
    With oChart
        .Type = chChartTypeLine
        .HasTitle = True
        .Title.Caption = "Daily OEE: Cell " & myCell
    End With
    Forms![frmChartOEEDaily].Caption = "Daily OEE: Cell " & myCell
    With axValueAxis
                .HasTitle = True
                .Title.Caption = "OEE"
                .Scaling.Minimum = 0
                .Scaling.Maximum = 1
                .NumberFormat = "#%"
                .MajorUnit = 0.1 ' 10%
    End With
    With axCategoryAxis
                .HasTitle = True
                .Title.Caption = "Date"
                '.NumberFormat = ""
                '.HasAutoMinimum = True
                '.HasAutoMaximum = True
    End With
'     ***  Delete series collection before adding new   ***
        With oChart
            i = .SeriesCollection.Count - 1
            For j = i To 0 Step -1
                .SeriesCollection.Delete (j)
            Next
 
            '   ***************************************************** _
                *   Create two series collections   * _
                *   1, For start time &     * _
                *   2, For duration     * _
                *****************************************************
            .SeriesCollection.Add
            .SeriesCollection.Add
            .SeriesCollection.Add
            .SeriesCollection.Add
            .SeriesCollection.Add
            .SeriesCollection(0).SetData chDimValues, chDataLiteral, arrE   ' OEE
            .SeriesCollection(0).SetData chDimCategories, chDataLiteral, arrA
            .SeriesCollection(1).SetData chDimValues, chDataLiteral, arrC   ' Performance
            .SeriesCollection(2).SetData chDimValues, chDataLiteral, arrB   'availability
            .SeriesCollection(3).SetData chDimValues, chDataLiteral, arrD   'Quality
            .SeriesCollection(4).SetData chDimValues, chDataLiteral, arrF   'Rolling average
            Set oSeries0 = .SeriesCollection(0) '   OEE
            Set oSeries1 = .SeriesCollection(1) '   Performance
            Set oSeries2 = .SeriesCollection(2) '   Availablity
            Set oSeries3 = .SeriesCollection(3) '   Quality
            Set oSeries4 = .SeriesCollection(4) '   Rolling average OEE
            .PlotArea.Interior.Color = RGB(230, 237, 215)
            .HasLegend = True
    End With
 
        oSeries0.Caption = "OEE"
        oSeries1.Caption = "Performance"
        oSeries2.Caption = "Availability"
        oSeries3.Caption = "Quality"
        oSeries4.Caption = "OEE Rolling average " & RA & " Days"
    '   ********************************** _
        *   Routine to highlight current date on graph  * _
        *   Using the OEE series                        * _
        *************************************************
    With oSeries0
            .Marker.Style = chMarkerStyleX
            .Marker.Size = 12
            For j = 0 To rsCount - 1
               .Points(j).Interior.Color = chColorNone
               .Points(j).Border.Color = chColorNone
             Next j
             If h > 1 Then ' if no oee score for the date selected then the pointer at the first position is chosen
                .Points(h).Interior.Color = "red"
            End If
   End With
        '   hide the first six data points as they are not a true _
            Rolling average - if displayed they provide an average _
            of the days: day 1 = day 1, day 2 = average(day1, day 2) etc.
        If RA > rsCount Then ' so no error occurs if there are less than RA records
            RA = rsCount
        End If
        With oSeries4
            .Line.DashStyle = chLineRoundDot
            For i = 1 To RA - 1
                .Points(i).Line.Color = chColorNone
            Next
            For i = RA To rsCount - 1
                .Points(i).Line.Color = "Cyan"
            Next
 
        End With
End Sub
 
Tim,

Thanks for your reply and your code. It will take me a while to make my way through it! The reason I waded into coding a chart was simply to manage the Y-axis so I didn't have flat graphs. I figured that out.

When I tried to modify lines (dotted, dashed, etc.) was when I found that I needed the .SeriesCollection(x). I see that you've created the whole thing from code - oh my! Do you know any way that I can just latch onto existing lines and modify them?

Thanks again,
Sherry
 
I am hoping this thread is still alive as I also am trying to manipulate an Access 2007 chart. I have my chart working great except for one small thing that is really not a big deal except to me. I already have created some reports that are based on graphic color coded icons. The colors in my other reports each represent a catagory of tasks. For example a green Icon represents "Flat Processing", a Yellow Icon represents "Hazmat Processing" and Blue Represent "Clothing Processing". I would like my chart to reflect the same colors in my bar graph everytime I run it. What is happening is it works great but each time it runs with a new set of colors each time for the bars and legend. Can you help me understand what approach I can take to keep the colors for each catagory consistant each time I run it. Do I have to create the chart in VBA as you have here or is there a way to just reference the Catagory axis and manipulate the Me.chart?
 
I'm using the Chart object in Access 2007, and the code throws an error when I try to use:

myChartObject.SeriesCollection.Add

No problem using myChartObject.SeriesCollection.Delete, but I'm trying to create a Chart from scratch through VBA, and my inability to add a new Series is driving me nuts.

Any suggestions would be so welcome!
MTIA
 
If i can recall correctly.
I had problems creating new series with the number of fields. I created a recordset RS...
Code:
[SIZE=3][FONT=Calibri]   With rs[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       rsCount = .RecordCount[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       rsFields = .Fields.Count[/FONT][/SIZE]
 
[SIZE=3][FONT=Calibri]   '   Debug.Print rsCount[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       If .RecordCount > 0 Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           .MoveFirst[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           ReDim myData(rsCount, rsFields)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           ReDim arrTemp(rsCount)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           ReDim arrName(rsFields)[/FONT][/SIZE]
 
[SIZE=3][FONT=Calibri]           myData = .GetRows(rsCount)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       Else:[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           MsgBox "No Data to graph", vbCritical + vbOKOnly, "No data"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        End If[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]....[/SIZE][/FONT]

I then ensured that all the fields were deleted before creating new ones

Code:
[SIZE=3][FONT=Calibri]   With oChart[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       .Type = chChartTypeColumnStacked[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       .HasTitle = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       .Title.Caption = strTitle[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       .HasLegend = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   '   ********* Delete existing series **********[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]              [COLOR=blue]i = .SeriesCollection.Count - 1[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]           For j = i To 0 Step -1[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]               .SeriesCollection.Delete (j)[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]           Next[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   '   ********** create new series from myData **********[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       [COLOR=blue]For j = 0 To (rsFields - 1)[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]           If j < rsFields - 1 Then[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]               .SeriesCollection.Add[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri][COLOR=blue]           End If[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           For i = 0 To (rsCount - 1)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]               arrTemp(i) = myData(j, i)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           '    Debug.Print arrTemp(i)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           Next i[/FONT][/SIZE]
 
[SIZE=3][FONT=Calibri]           If j = 0 Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]               .SeriesCollection(j).SetData chDimCategories, chDataLiteral, arrTemp ' first column is categories[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           Else[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]               .SeriesCollection(j - 1).SetData chDimValues, chDataLiteral, arrTemp[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]               .SeriesCollection(j - 1).Caption = arrName(j)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]           End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       Next j[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   End With[/FONT][/SIZE]

Hope this helps
 
Have you tried to specify all the colors in VBA for all your fields? Otherwise, I guess that the system defaults will take over?

T
 
Thanks Tim, I'll try deleting all SeriesCollection objects first.

Quick question: the only info I can find for the Office 2007 MS Graph Object Model is the help file vbagr10.chm, and at MSDN online. However, I can't find any info on the SetData method (etc). Could you point me to some decent info on the Object Model pls?
 
Try
http://support.microsoft.com/kb/186855

This has a access 97 graph with vba attached

Other than that, it is hard to find data & a good object model. :banghead:

:)

I did try Microsoft Web Components but this had its own set of problems.

Good luck and if you find something please post the results:D



Tim
 

Users who are viewing this thread

Back
Top Bottom