A.D.Tejpal
adtp at airtelmail.in
Sat Aug 9 08:51:45 CDT 2008
Label captions in subreport - dynamic assignment ================================= Darryl, In case of reports, time flow has to be kept in view. Open event of subreport fires later than that for the parent report. Run time assignment of label captions (or values for other controls) in a report has to be carried out in events belonging to THAT report. This implies that in case of a subreport, you have to use an event of the report serving as source object for the subreport control in question. Any attempt to do so, directly in open event of the parent report itself leads to error 2455 (Invalid reference to property - Form/Report). This is because, at the open event stage of parent report, various controls (including the subreport control) are still in a state of flux, and do not yet expose the full range of their properties. Moreover, at this juncture, even the open event of subreport itself is yet to fire (the subreport is thus not yet effectively available at this stage). Following alternatives can be considered for run time assignment of label captions in a subreport. The term subreport represents the report used as source object for the subreport control. LbBudget is the name of label on subreport's report header: 1 - Use value held by a form control: ------------------------------------- This method for assigning caption for subreport's label is simplest and straightforward. Sample code in subreport's open event is given below : ' Code in subreport's module '======================================= Private Sub Report_Open(Cancel As Integer) Me.LbBudget.Caption = _ Forms("F_Report")("TxtCaption") ' Note - TxtCaption is the name of text box ' on form F_Report, holding the desired ' caption string (Form F_Report should ' be in open state). End Sub '======================================= 2 - Pass OpenArgs value to main report: ----------------------------------------- Assign the caption string for subreport's label as per value of OpenArgs passed on to the parent report. This method too is simple and straightforward, but available only in versions later than Access 2000. Sample code in command button's click event and subreport's open event is given below : ' Code in form's module ' (CmdReport is the name of command button) '======================================= Private Sub CmdReport_Click() DoCmd.OpenReport "R_Main", _ acViewPreview, , , , "<<MyCaptionString>>" DoCmd.Maximize DoCmd.RunCommand acCmdZoom100 ' Note - R_Main is the name of parent report. End Sub '======================================= ' Code in subreport's module '======================================= Private Sub Report_Open(Cancel As Integer) Me.LbBudget.Caption = Me.Parent.OpenArgs End Sub '======================================= 3 - Use a caption string generated in parent report's open event: --------------------------------- Assign the caption on subreport's label as per a string explicitly declared in parent report's open event. This method uses report level public global variable in the parent report. Normally, either of alternatives 1 or 2 mentioned earlier should suffice. Use of parent report's open event for determining subreport label's caption could be considered only if the intended string is going to depend upon code in parent report's module. Sample code in parent and subreport's modules is given below : ' Code in parent report's module '======================================= ' Declarations section Public CapString As String ----------------------------------------------------------- Private Sub Report_Open(Cancel As Integer) CapString = "<<MyCaptionString>>" End Sub '======================================= ' Code in subreport's module '======================================= Private Sub Report_Open(Cancel As Integer) Me.LbBudget.Caption = Me.Parent.CapString End Sub '======================================= Important: ---------- In a parallel situation, if values to subreport's text boxes were to be assigned at run time (as compared to label captions), subreport's open event would no longer be suitable. Any attempt to do so would trigger error -2147352567 (80020009) "You can't assign a value to this object." Appropriate section event subsequent to report's open event (e.g. report header's format event) can be used for this purpose. You might like to adopt the alternative considered most convenient for your situation. Best wishes, A.D.Tejpal ------------ ----- Original Message ----- From: Darryl Collins To: 'Access Developers discussion and problem solving' Sent: Friday, August 08, 2008 12:00 Subject: [AccessD] Sub Report Syntax... Hi all, A dead easy one I am sure, but got a bad case of fried brain this friday afternoon. What is up with this syntax... [Reports]![rpt_ProjectSummary].[rpt_TL1].[Report].Budget.Caption = sBudget is returning "Invalide Ref to Property Form/Report", which would suggest a spelling or naming error, but I check that about 20 times already. Or do have the syntax all buggered for the subreport. I want to write the value of the string "sBudget" into a label (named budget) on the subreport. Can be that hard surely!!?? :) cheers Darryl.