Access VBA Macro Programming

(Joao Candeias) #1
DoCmd.Close acReport, "rptTemplate"
RepName = rep.Name

rep.OLEUnbound0.RowSource = "tblChart"
rep.OLEUnbound0.RowSourceType = "Table/Query"
rep.OLEUnbound0.ColumnHeads = True
rep.OLEUnbound0.Top = 100
rep.OLEUnbound0.Left = 100

DoCmd.Restore
DoCmd.Save acDefault
DoCmd.Close acDefault

DoCmd.Rename "rptMyGraph", acReport, RepName
DoCmd.OpenReport "rptMyGraph", acViewDesign
DoCmd.Restore

Set ch = Reports("rptMyGraph").OLEUnbound0.Object
ch.HasTitle = True
ch.ChartTitle.Text = "My Chart"
ch.ChartType = xl3DPie

DoCmd.Save acDefault
DoCmd.Close acDefault
DoCmd.OpenReport "rptMyGraph", acViewReport
End Sub

This code will create a new report, add a chart to it, save it, and rename it, and then
manipulate the parameters of the chart.
The code first sets up objects for the new report and control, as well as for the chart based
on the Graph Object library. The report rptTemplate that you created earlier is opened in
Design mode and is kept hidden.
TheCreateReportmethod is used to create a new report, and thenCreateReportControl
is used to create an object frame in the Detail section of that report. The object frame OLEData
is then set to the OLEData from the chart in rptTemplate. This effectively makes it into a
Chart control.
The name of the report is stored in a string, since we wish to rename the report and this
cannot be done while it is open.
Properties are then set for the OLEObject for RowSource, RowType, Top, and Left.
ColumnHeads must be set to True, otherwise the first row of your source table will be left out.

234 Microsoft Access 2010 VBA Macro Programming

Free download pdf