Using Multiple Instances of a Sub-Report

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Sorry for the cross-posting, but I didn't get any response in the General
Forum...
====================================================

I know how to access properties of sub-forms from other objects (see
sample code below), but I can't figure out how to do the same thing with a
sub-report.

Forms!frmParent!frmChild.Form.RecordSource = ""

I'd also like to know how to pass a value from the parent report to the
sub-report (I want to use two instances of the same sub-report, but they will
display different results depending on the value passed to them).

Linking parent/child fields won't work in this case, I need some way for the
report to tell each copy of the subreport which criteria to use.

Thanks, Dave
 
SlydeRule said:
I know how to access properties of sub-forms from other objects (see
sample code below), but I can't figure out how to do the same thing with a
sub-report.

Forms!frmParent!frmChild.Form.RecordSource = ""

I'd also like to know how to pass a value from the parent report to the
sub-report (I want to use two instances of the same sub-report, but they will
display different results depending on the value passed to them).

Linking parent/child fields won't work in this case, I need some way for the
report to tell each copy of the subreport which criteria to use.


No can do. A subreport can only use the query it was
provided in its first instance.

However, the Link Master proerty can refer to a text box on
the main report so you can set the filtering value into the
text box in the mainreport. Similarly, a criteria in the
subreport's record source can refer to a main report text
box using the usual syntax: Reports!mainreport.sometextbox

If that's not sufficient, then I would need the gory details
of what you are trying to accomplish before I could
determine a way to get it done or decide that it can't be
done using any normal approach.
 
In addition to Marsh's sage comments, you might be able to use multiple
subreports and choose to hide/display them as needed.
 
Hi Marshall, thanks for the reply. Since the subreport won't know which
instance it is (1st or 2nd or whatever), it wouldn't know which textbox on
the parent report to look at. Your first suggestion (Link Master Field)
might work though, if I can tell from the subreport which textbox it is
linked to.

In short, I need to commuincate a different piece of information from the
report to each copy of the subreport, so the subreport will know what it's
supposed to do.

Thanks...

Dave
 
SlydeRule said:
Hi Marshall, thanks for the reply. Since the subreport won't know which
instance it is (1st or 2nd or whatever), it wouldn't know which textbox on
the parent report to look at. Your first suggestion (Link Master Field)
might work though, if I can tell from the subreport which textbox it is
linked to.

In short, I need to commuincate a different piece of information from the
report to each copy of the subreport, so the subreport will know what it's
supposed to do.


I guess I need to see the long version of what you're trying
to do. There may be different approaches depending on which
version of Access you're using, so include that info too.
 
Hi Marshall, thanks again for your interest in helping me with this. OK,
here's the 'long' story...

First off, I do have the report working by using 4 separate subreport files,
each with identical code with the exception of the value of one variable. So
my interest in doing this is just to eliminate 3 copies of duplicate code.

We have a table which keeps track of which employees are late in submitting
their time sheets and work plans each week. There is a userid field, a yes/no
field and a notes field for each (time sheet and work plan), so it is a
simple table. The query used by the report calulates the number and percent
a person was late submitting each item over a 10 week period and a 52 week
period.

The report shows this data in 4 different formats:
1 - Time Sheets submitted late- Sorted (descending) by the percent late over
the past 10 weeks.

2 - Time Sheets submitted late- Sorted (descending) by the percent late over
the past 52 weeks.

3 - Work Plans submitted late- Sorted (descending) by the percent late over
the past 10 weeks.

4 - Work Plans submitted late- Sorted (descending) by the percent late over
the past 52 weeks.

So, basically, the subreport needs to know which one of these formats to use.

I did think of another option. I could create a simple report (no
subreports embedded) and call it 4 times with different OpenArgs. However,
management prefers it all in one report, as it is emailed to several people
(one attachment vs four).

So that's the long version. I'd appreciate your suggestions...

Dave
 
Well, If I'm following that correctly, the query is doing
the calculations so all you need in the report is to specify
the Sorting, which you intend to use OpenArgs to tell the
report the name of the calculated field to use.

If that's the case, then you can use code in the subreport's
Open event procedure. Here's a simple outline of what the
code would look like:

Sub Report_Open()
Static Initilized As Boolean

If Not Initialized Then
Me.GroupLevel(0).ControlSource = Parent.OpenArgs
Initialized = True
End If
End Sub

The reason for the Initialized stuff is that you can only do
this kind of thing the first time the subreport appears.
Once the subreport starts, it will not allow changes of this
nature.

You may have additional needs here, such as only displaying
the sort field (as opposed to all the calculated fields).
If so, then just add another line to the procedure to set
the text box's control source.
 
Hi Marshall, thanks again for the help.

The report, as it is now, doesn't have any OpenArgs. I intended to use them
only if I combined everything into a single report (no subreport) and ran it
4 times. If I use OpenArgs in my current design, all four instances of the
subreport would read the same thing.

The key point is, I need to 'push' the info to each subreport, rather than
have them 'pull' it from the parent.

I coded the subreport to dynamically set its control sources, sort order,
column titles, and more based on a single variable, which could have 4
different values ("TS10", "TS52","WP10", or "WP52"). I need a way to tell
each copy of the subreport which one of these values to use.

Thanks...

Dave
 
Apparently, I have misunderstood what you mean by an
instance. I thought you had one subreport control on the
main report and it would appear in multiple details.

Now, it think what you're describing is four subreport
controls, all with the same source object.

The goal, as I now understand it, is to have each subreport
control's subreport sorted, etc. in a different way. This
situation is not amenable to what to any parameter passing
technique, whether it be a pull or push technique (which is
propably why you posted your question ;-)

AFAIK, a subreport can not determine its container subreport
control. Someone else may have another idea, but the only
thing I can come up with is to use the control's Z-order.
In this case the subreport's are "opened" in the order that
the subreport controls were added to the main report. Then
the subreport's open event procedure can check each instance
on the main report to "see" which one is executing now.

To do all that, set the design time sorting field to some
field not involved in the various sorts such as the ID
field. Then use code along these lines in the subreport's
Open event:

Static Initialized As Boolean

If Not Initialized Then
If Parent.subTS10.Report.GroupLevel(0).ControlSource _
= "ID" Then
Me.GroupLevel(0).ControlSource = "TimeLate10"
ElseIf Parent.subTS52.Report.GroupLevel(0).ControlSource
= "ID" Then
Me.GroupLevel(0).ControlSource = "TimeLate52"
ElseIf Parent.subWP10.Report.GroupLevel(0).ControlSource
= "ID" Then
Me.GroupLevel(0).ControlSource = "WorkLate10"
ElseIf Parent.subWP52.Report.GroupLevel(0).ControlSource
= "ID" Then
Me.GroupLevel(0).ControlSource = "WorkLate52"
End If
Initialized = True
End If

While that appeared to work in my tests, I think it relies
on a very "delicate" set of premises that are not documented
and I would not use it.

I think what I would do is use four different report objects
for the subreports. The code can be identical in all four
report objects by using Me.Name to distinguish which one is
currently executing. At least this way you do not have to
maintain four different report objects. When a change is
made to one copy, you just have to remember to delete the
other, now out of date, three report objects and copy the
new version to the other three names.
 
Marshall, thanks very much for taking the time to explain this to me. I will
try out your suggestion. It's a shame they didn't design it to work the same
way as a subform.

Thanks again...

Dave
 
Back
Top