Excel Charts in Report

  • Thread starter Thread starter Peter Hafner
  • Start date Start date
P

Peter Hafner

Hi there,

I want to visualize data in my reports with MS-Excel charts. How do I
link the embedded Excel object with access data?

Thanx,

Peter
 
Peter,

While in design mode in your Access report, go to the menu
and choose Insert, Chart. Access will build the chart in
the report based on the report data. It's a little
finicky, but it works.

Good Luck,

Wayne
 
Is there a particular reason why you want to use Excel charts and not Access
charts?
 
Duane said:
Is there a particular reason why you want to use Excel charts and not Access
charts?
Yes,
I feel that the Access charts are too limited to present scientific
data. I.e. I couldn´t find a way to combine two charts into one.

Peter
 
Access charts allow you to graph two different scales at the same time. One
scale is "ticked" on the left and one is "ticked" on the right.
 
Duane said:
Access charts allow you to graph two different scales at the same time. One
scale is "ticked" on the left and one is "ticked" on the right.
One thing I need to do: given a bar chart of a group totlal I want to
show the average, min and max for a given group member. In excel chart
this can be realized with x-error bars. In access chart there are no
x-error bars, only y-error bars. Or am I missing something.
Another reason to use excel is the possibility to make complex
statistical calculations with data before charting.

Peter
 
Peter,


Automation help for manipulating Excel from Access at MsKb:
302460 Office XP Products
260410 Office 2000 Products


From the Graph10.chm help file:

Chart types that support error bars:
You can add error bars to data series in 2-D area, bar, column, line, xy
(scatter), and bubble charts. For xy (scatter) and bubble charts, you can
display error bars for the x values, the y values, or both.

Add error bars to a data series:
Click the data series to which you want to add error bars.
On the Format menu, click Selected Data Series.
On the X Error Bars tab or the Y Error Bars tab, select the options you
want.
Note: If you change the datasheet values associated with the data points
in the series, the error bars are adjusted to reflect your changes.



From VBAGR10.chm:

Using the ErrorBars Object
Use the ErrorBars property to return the ErrorBars object. The following
example turns on error bars for series one in myChart and then sets the end
style for the error bars.

myChart.SeriesCollection(1).HasErrorBars = True
myChart.SeriesCollection(1).ErrorBars.EndStyle = xlNoCap

ErrorBar Method:
Applies error bars to the specified series. Variant.

expression.ErrorBar(Direction, Include, Type, Amount, MinusValues)
expression Required. An expression that returns one of the objects in the
Applies To list.

Direction: Required XlErrorBarDirection. The error bar direction.

XlErrorBarDirection can be one of these XlErrorBarDirection constants.
xlX Can only be used with scatter charts.
xlY default.

Include: Required XlErrorBarInclude. The error bar parts to be included.

XlErrorBarInclude can be one of these XlErrorBarInclude constants.
xlErrorBarIncludeBoth default.
xlErrorBarIncludeMinusValues
xlErrorBarIncludeNone
xlErrorBarIncludePlusValues

Type: Required XlErrorBarType. The error bar type.

XlErrorBarType can be one of these XlErrorBarType constants.
xlErrorBarTypeCustom
xlErrorBarTypeFixedValue
xlErrorBarTypePercent
xlErrorBarTypeStDev
xlErrorBarTypeStError

Amount: Optional Variant. The error amount. Used for only the positive
error amount when Type is xlErrorBarTypeCustom.

MinusValues: Optional Variant. The negative error amount when Type is
xlErrorBarTypeCustom.

Example
This example applies standard error bars in the Y direction for series one.
The error bars are applied in the positive and negative directions. The
example should be run on a 2-D line chart.

myChart.SeriesCollection(1).ErrorBar _
Direction:=xlY, Include:=xlErrorBarIncludeBoth, _
Type:=xlErrorBarTypeStError


End of Error Bar info, on to Excel formulas...
Most of the "fancy" Excel formulas are available for use in Access via
Automation or referencing an Excel library, examples below:

Public Sub XIRRTest()
Dim objExcel As Excel.Application
Set objExcel = CreateObject("Excel.Application")

Dim p(4) As Double
p(0) = -10000
p(1) = 2750
p(2) = 4250
p(3) = 3250
p(4) = 2750

Dim d(4) As Date
d(0) = DateValue("1/1/1998")
d(1) = DateValue("3/1/1998")
d(2) = DateValue("10/30/1998")
d(3) = DateValue("2/15/1999")
d(4) = DateValue("4/1/1999")

Debug.Print objExcel.Application.XIRR(p, d)

objExcel.Quit
Set objExcel = Nothing

End Sub

Public Function fXIRR(ByRef arFlows, ByRef arFlowdates, Optional dblGuess As
Double = 0.100000001490116) As Double
'With a reference set to Office Web Components - msowcf.dll
'All of the nifty functions available in Excel are available

Dim oFunc As MSOWCFLib.OCATP

Set oFunc = New MSOWCFLib.OCATP
fXIRR = oFunc.XIRR(arFlows, arFlowdates, 0.05)

Set oFunc = Nothing

End Function



Now if you want to do the calculations in Excel, one technique would be to
use the Range CopyFromRecordset method shown below to fill a datarange in an
Excel worksheet.

Public Sub CopyRs2Sheet(strSql As String, strWorkBook As String, _
Optional strWorkSheet As String, Optional strCellRef
As String)
'Uses the Excel CopyFromRecordset method
'strSql: Sql Select string
'strWorkBook: Full path and name to target wb, will create if doesn't
exist
'strWorkSheet: Name of target worksheet, will create if doesn't exist
'strCellRef: Upper Left cell for data, defaults to A1


On Error GoTo ProcError
DoCmd.Hourglass True

Dim objXLApp As Object 'Excel.Application
Dim objXLWb As Object 'Excel.Workbook
Dim objXLSheet As Object 'Excel.Worksheet
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim i As Integer
Dim iSheets As Integer

'set rs from sql, table or query
Set rs = CurrentDb.OpenRecordset(strSql, dbOpenSnapshot)

'start Excel
Set objXLApp = CreateObject("Excel.Application")

'open workbook, error routine will
'create it if doesn't exist

'only create workbooks with 1 sheet
iSheets = objXLApp.SheetsInNewWorkbook 'save user's setting
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = iSheets 'restore user's setting

'select a worksheet, if sheet doesn't exist
'the error routine will add it
If strWorkSheet = "" Then
strWorkSheet = "Sheet1"
End If

'If Range is missing default to A1
If strCellRef = "" Then
strCellRef = "A1"
End If

'select desired worksheet
Set objXLSheet = objXLWb.Worksheets(strWorkSheet)

'insert recordset into Excel Worksheet using CopyFromRecordset method
objXLSheet.Range(strCellRef).CopyFromRecordset rs
objXLSheet.Columns.AutoFit

'Save wb
objXLWb.Save
objXLWb.Close

'close up other rs objects
If Not rs Is Nothing Then rs.Close
Set rs = Nothing

Set objXLSheet = Nothing
Set objXLWb = Nothing

'quit Excel
If Not objXLApp Is Nothing Then objXLApp.Quit
Set objXLApp = Nothing

DoCmd.Hourglass False
Exit Sub

ProcError:

Select Case Err
Case 9 'Worksheet doesn't exist
objXLWb.Worksheets.Add
Set objXLSheet = objXLWb.ActiveSheet
objXLSheet.NAME = strWorkSheet

Resume Next

Case 1004 'Workbook doesn't exist, make it
objXLApp.Workbooks.Add
Set objXLWb = objXLApp.ActiveWorkbook
objXLWb.SaveAs strWorkBook

Resume Next

Case Else
DoCmd.Hourglass False
MsgBox Err.Number & " " & Err.Description
Stop
Resume 0
End Select

End Sub


Hope it helps...

Steve
 
Steve, thanks a lot.
The hint with the Graph10.chm helped me a lot. I now have it almost
running. The only problem is, that the excel chart is not refreshing,
although the workbook is updated propperly.
Any more ideas on that?
 
Peter,

Is this a linked graph or inserted object?
I'm at a bit of a disadvantage, you haven't posted any code... have you
asked the chart object to Refresh?

Steve
 
Steve,
I tried to update the Report as follows:
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
....
Me.Chart.Requery
....
End Sub

Refresh is not possible.

Peter
 
Dim objChart as Object
Set objChart = Me.Chart
objChart.Refresh
'This will update the data sheet
objChart.Application.Update
 
Sorry,
but this gives me this error: object doesn't support this property or method
What is wrong with my object?
Any more ideas?

Peter
 
Peter:

Guess I forgot to put in part of the code (BTW it needs to be run in the
same section of the report that contains the graph.

Dim objChart as Object
Set objChart = Me.Chart.Object
objChart.Refresh
 
Peter,

I have inserted an Excel Graph onto a form and I'm having a problem having
it "Refresh" also.

Doesn't seem to respond to the following either:
Me.Chart.Action = 6 '6 = acOLEUpdate

Sure you don't want to use MsGraph?

Steve
 
Steve,

I cant make it work. I also tried a Excel Table and it updates perfectly
without any code. Seems to be a bug in the Excel-Chart object.
Unfortunately MS-Graph does not let me define my own Range of error in
X-error bars.
Looks like I'm totally lost.
Peter
 
I've not used ErrorBars, however the help shows Amount and MinusValues, is
that what you need?

Steve
Direction Required XlErrorBarDirection. The error bar direction.

XlErrorBarDirection can be one of these XlErrorBarDirection constants.
xlX Can only be used with scatter charts.
xlY default.

Include Required XlErrorBarInclude. The error bar parts to be included.

XlErrorBarInclude can be one of these XlErrorBarInclude constants.
xlErrorBarIncludeBoth default.
xlErrorBarIncludeMinusValues
xlErrorBarIncludeNone
xlErrorBarIncludePlusValues

Type Required XlErrorBarType. The error bar type.

XlErrorBarType can be one of these XlErrorBarType constants.
xlErrorBarTypeCustom
xlErrorBarTypeFixedValue
xlErrorBarTypePercent
xlErrorBarTypeStDev
xlErrorBarTypeStError

Amount Optional Variant. The error amount. Used for only the positive
error amount when Type is xlErrorBarTypeCustom.

MinusValues Optional Variant. The negative error amount when Type is
xlErrorBarTypeCustom.
 
Hi Steve:

Thanks for the hint. I tried xlErrorBarTypeCustom but it failes with:
ErrorBar method of Series class failed - error. Also in the user
interface there is no option Custom Type for Errorbar (In Excel Chart it
is!). So onec again it is not working the way I want to.
Thanks anyway for your valuable input!

Peter
 
Back
Top