Home Subreports

Cloning a subreport at runtime

edited November 2002 in Subreports
I found a solution I'd like to share since I haven't seen it written up
anywhere. Thanks to Jim Bennett and Ed Dressel for pointing me in the right
direction.

I had created a report with a subreport at design-time. At runtime I wanted
a report with several copies of the subreport, using different datasets.

One solution is to create the subreport in code. This is clumsy because then
you can't use the RBuilder designer to modify the report.

A second solution is to save the subreport's template to a file or a
database. This is better but assumes your app has a file or database to
save to, and can raise installation issues if it's a shipping app.

The solution I finally came up with is to clone the copy of the subreport
that's stored in the DFM. One way is to subclass the SubReport class:

TMySubReport = class(TppSubReport)

and add a method like:

procedure TMySubReport.CopyTemplateFrom(ASubReport :
TppSubReport);
var
lMemoryStream : TMemoryStream;
begin
lMemoryStream := TMemoryStream.Create();
try
ASubReport.Report.Template.SaveToStream(lMemoryStream);
lMemoryStream.Position := 0;
Report.Template.LoadFromStream(lMemoryStream);
finally
lMemoryStream.Free();
end;
end;

Then you can create a TMySubReport, and call its CopyTemplateFrom method,
passing in the subreport that was defined at design-time and saved in the
DFM. Hooking these subreports to the main report and to the different
datasets worked for me.

Mike Carroll
Sigma Data Systems, Inc.

Comments

This discussion has been closed.