Unanswered: Creating and customising a report in VBA
I'm trying to make an application "easy" to maintain, by anticipating changes that might need to be made in the future and automating them as much as possible.
So, I have a bunch of nearly-identical reports which are all subreports to a big report. The change I wish to anticipate is that a new subreport, similar to but not quite the same as the others, needs to be added. I can't just create it now because I don't know the value of some key attributes yet - but somebody will when they come to push my magic buttons.
So I get as far as:
NewName = <constructed name for the new subreport>
DoCmd.CopyObject , NewName, acReport, "subreportTemplate"
... which creates a new report, copied from the template, with the right name.
I'd now like to "fix" a couple of the attributes of this report, say the value of a Label and a Filter string.
How can I do this? I've been blundering about Opening the form and then trying to set properties via Forms! ... but I'm being told the form doesn't exist, even though I can see that it is in fact being created.
If you want to access to the properties of a Report object, you have to address this object through the Reports collection, not the Forms collection. Moreover, don't forget that a Form or Report object is only present in its generic collection (Forms for a Form object, Reports for a Report object) whent it's open. So:
Function HandleReport(ByVal ReportName As String)
Dim rpt As Report
DoCmd.OpenReport ReportName, acViewDesign
Set rpt = Reports(ReportName)
' Handle rpt properties and methods.
.Caption = "My Report"
.AutoResize = True
Set rpt = Nothing
DoCmd.Close acReport, ReportName, acSavePrompt