Home Subreports
New Blog Posts: Merging Reports - Part 1 and Part 2

deleting a subreport at runtime

edited August 2006 in Subreports
I have a subreport that I sometimes do not want included in a report. In
the past I just called FreeAndNil on the subreport component and
everything worked OK.

I have now switched to loading a report template, so instead of deleting
the subreport component that is declared in my form, I have to use
GetSubReports to get a reference to the subreport. When using this method,
if I free the subreport object it still gets referenced by report builder
later when the report is printing, and causes an exception.

Is there another way I can prevent the subreport from being included at
runtime?

TIA,
J.D. Mullin
Advantage R&D



--- posted by geoForum on http://delphi.newswhat.com

Comments

  • edited August 2006
    I have found a work-around for now. I created an empty in-memory table
    dataset component, a datasource, and at runtime I point my subreport to
    this empty dataset. Not as elegant as I was hoping for, but it works for now.

    J.D.



    --- posted by geoForum on http://delphi.newswhat.com
  • edited August 2006
    Hi JD,

    Once the template has loaded, you could use a report object loop to gain
    access to the subreport object, then either set its visibility to False and
    DataPipeline to nil, or completely remove it from the report by freeing it.
    Take a look at the following article on creating and using a report object
    loop.

    ----------------------------------------------
    Tech Tip: Loop Thru All Objects in a Report
    ---------------------------------------------

    A ReportBuilder report is composed of a set of components. The basic
    structure is

    Reports.Bands[].Objects[]

    The bands and objects within the report can be accessed directly by object
    name or via the Bands and Objects array properties.

    Below is an example of using the Bands and Objects array properties to
    change the font for all objects on a report.


    uses
    ppClass;


    procedure AssignFontToReport(aFont: TFont; aReport: TppCustomReport);
    var
    liBand: Integer;
    liObject: Integer;
    lObject: TppComponent;

    begin

    for liBand := 0 to aReport.BandCount-1 do

    for liObject := 0 to aReport.Bands[liBand].ObjectCount-1 do
    begin
    lObject := aReport.Bands[liBand].Objects[liObject];

    if lObject.HasFont then
    lObject.Font := aFont;

    end;

    end;


    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited August 2006
    Thanks Nico,

    I have a pointer to the subreport TppReport object, but I wasn't able to
    find out how to set its visiblity. I couldn't find a visible property or a
    method to set visibility.

    I use the following code to get the subreport:

    reportFields.GetSubReports( oSubReports );
    oTablePropReport := TppReport( oSubReports.Objects[0] );

    I'm sure I'm missing something simple to change the visibility. Any help
    would be appreciated.

    Thanks for your time,
    J.D.





    --- posted by geoForum on http://delphi.newswhat.com
  • edited August 2006
    Hi J.D.,

    The GetSubReports routine gives you a handle to each TppChildReport object
    (a TppCustomReport descendent and essentially the subreport design). Each
    TppChildReport is tied to a TppSubreport object which is the actual
    component you place on the main report. You will need to set the visibility
    of this component rather than the actual Child Report. I would recommend
    using a report object loop to gain access to the subreport object unless you
    know the actual subreport you wish to render invisible.

    ----------------------------------------------
    Tech Tip: Loop Thru All Objects in a Report
    ---------------------------------------------

    A ReportBuilder report is composed of a set of components. The basic
    structure is

    Reports.Bands[].Objects[]

    The bands and objects within the report can be accessed directly by object
    name or via the Bands and Objects array properties.

    Below is an example of using the Bands and Objects array properties to
    change the font for all objects on a report.


    uses
    ppClass;


    procedure AssignFontToReport(aFont: TFont; aReport: TppCustomReport);
    var
    liBand: Integer;
    liObject: Integer;
    lObject: TppComponent;

    begin

    for liBand := 0 to aReport.BandCount-1 do

    for liObject := 0 to aReport.Bands[liBand].ObjectCount-1 do
    begin
    lObject := aReport.Bands[liBand].Objects[liObject];

    if lObject.HasFont then
    lObject.Font := aFont;

    end;

    end;


    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.