I have code I wrote that automates an Excel application and MS Graph via an Event Procedure in Access (this is all done with Office 2000).
Whenever I run the code, I watch the Excel application open, the first sheet is populated with the data, and then I watch it automatically generate the graph. Just as the graph starts to be created it blanks out and I receive the following error in Access:
run-time error 1004
Method 'SeriesCollection' of object '_Chart' failed
The code I am running is as follows:
If Me!ClientSelectionGroup = 10 Then
If IsNull(Me!StartDate) Xor IsNull(Me!EndDate) Then
MsgBox "You must specify both a Start Week and an End Week."
Exit Sub
End If
If IsNull(Me!StartDate) And IsNull(Me!EndDate) Then
FilterStr = ""
Else
FilterStr = " WHERE ReportDate Between #" & Me!StartDate & "# and #" & Me!EndDate & "#"
End If
If IsNull(Me!xFeldList) Then
MsgBox "Please select a x field to create a chart for."
Exit Sub
End If
TableStr = "TableName"
FieldStr = Me!xFieldList.Column(0)
FieldNameStr = Me!xFieldList.Column(1)
PeriodTypeStr = "ReportDate"
PeriodTypeNameStr = "Date"
End If
SQLstr = "SELECT " & PeriodTypeStr & ", " & FieldStr & " FROM " & TableStr & FilterStr & " ORDER BY " & _
PeriodTypeStr & " DESC"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQLstr)
Set xl = CreateObject("Excel.Application")
If rs.RecordCount > 0 Then
With xl
.Visible = True
.UserControl = False
Set objWkb = .Workbooks.Add
Set objSht = objWkb.Worksheets(1)
With objSht
.Cells(2, 1).CopyFromRecordset rs
End With
Range("A1").Select
.ActiveCell.FormulaR1C1 = PeriodTypeStr
.ActiveCell.Font.Bold = True
Range("B1").Select
.ActiveCell.FormulaR1C1 = FieldStr
.ActiveCell.Font.Bold = True
Range("A2").Select
i = 1
Do
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop Until IsEmpty(ActiveCell)
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B" & i), PlotBy _
:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "=Sheet1!R2C1:R" & i & "C1"
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = FieldNameStr
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = PeriodTypeNameStr
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = FieldNameStr
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowNone, LegendKey:=False
ActiveChart.HasDataTable = False
End With
Else
MsgBox "No records meet your current criteria."
End If
Thank you for any help you can provide.
~Landar
Whenever I run the code, I watch the Excel application open, the first sheet is populated with the data, and then I watch it automatically generate the graph. Just as the graph starts to be created it blanks out and I receive the following error in Access:
run-time error 1004
Method 'SeriesCollection' of object '_Chart' failed
The code I am running is as follows:
If Me!ClientSelectionGroup = 10 Then
If IsNull(Me!StartDate) Xor IsNull(Me!EndDate) Then
MsgBox "You must specify both a Start Week and an End Week."
Exit Sub
End If
If IsNull(Me!StartDate) And IsNull(Me!EndDate) Then
FilterStr = ""
Else
FilterStr = " WHERE ReportDate Between #" & Me!StartDate & "# and #" & Me!EndDate & "#"
End If
If IsNull(Me!xFeldList) Then
MsgBox "Please select a x field to create a chart for."
Exit Sub
End If
TableStr = "TableName"
FieldStr = Me!xFieldList.Column(0)
FieldNameStr = Me!xFieldList.Column(1)
PeriodTypeStr = "ReportDate"
PeriodTypeNameStr = "Date"
End If
SQLstr = "SELECT " & PeriodTypeStr & ", " & FieldStr & " FROM " & TableStr & FilterStr & " ORDER BY " & _
PeriodTypeStr & " DESC"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQLstr)
Set xl = CreateObject("Excel.Application")
If rs.RecordCount > 0 Then
With xl
.Visible = True
.UserControl = False
Set objWkb = .Workbooks.Add
Set objSht = objWkb.Worksheets(1)
With objSht
.Cells(2, 1).CopyFromRecordset rs
End With
Range("A1").Select
.ActiveCell.FormulaR1C1 = PeriodTypeStr
.ActiveCell.Font.Bold = True
Range("B1").Select
.ActiveCell.FormulaR1C1 = FieldStr
.ActiveCell.Font.Bold = True
Range("A2").Select
i = 1
Do
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop Until IsEmpty(ActiveCell)
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B" & i), PlotBy _
:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = "=Sheet1!R2C1:R" & i & "C1"
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = FieldNameStr
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = PeriodTypeNameStr
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = FieldNameStr
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowNone, LegendKey:=False
ActiveChart.HasDataTable = False
End With
Else
MsgBox "No records meet your current criteria."
End If
Thank you for any help you can provide.
~Landar