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

Copy report at runtime?

edited September 2002 in General
I know this is kind of an object-pascal question, but it's coming up
related to RB:

I have a report created at design-time with multiple groups. I delete
some of the groups at run-time, based on how many groups the user has
selected on a form. After running the report, if the user changes the
groups desired and runs the report again, it bombs because some of the
groups are already deleted.

I'd like to operate and run on a copy/instance of the design-time report
at runtime, so that I can re-initialize it for the next run (without
closing the form).

TIA...

Comments

  • edited September 2002
    Save the report layout to a report template (rtm file) at design time. Then
    load the template at runtime when you want to restore the report to the
    original version with all of the groups defined.


    Cheers,

    Jim Bennett
    Digital Metaphors

  • edited September 2002
    That would force me to distribute another file with my application,
    though, which I really try to avoid (it means updating the setup routine
    and a definite new support issue).

    Any other way to do it?



  • edited September 2002
    Hi Richard,

    I use another TReport to accomplish a similar thing. Save the layout at
    run-time into a memory stream and load that stream into the new TReport.
    The user then edits the new TReport.

    Cheers

    Dean

  • edited September 2002
    Got a code sample?

    I get the gist of what you're saying but don't know the methods involved.

  • edited September 2002
    Okay, didn't take long, I figured it out:

    MyReport.template.savetostream...

    Thanks. I'm going to try saving it, deleting the groups, running the
    report, and then just restoring it back to the same report again...

  • edited September 2002
    Can't get it to work.

    In the unit's implementation, I have:

    msSaveReport : TMemoryStream ;

    In the unit's formcreate, I have:

    msSavePreport := TMemoryStream.create ;
    MyReport.template.saveToStream(msSaveReport) ;

    When the user clicks the run report button, I have:

    msSaveReport.position := 0 ;
    MyReport.template.loadFromStream(msSaveReport) ;

    However, this "works" the first time the report is run but I get an av
    the second time on the loadfromstream line.

    When I modify the code to read:

    MyReport.template.new ;
    msSaveReport.position := 0 ;
    MyReport.template.loadFromStream(msSaveReport) ;

    I get an av on the template.new line.
  • edited September 2002
    Okay, got it - there was a MyReport.free lurking from a previous idea of
    how to do this. Killed it an now the code, with the template.new line,
    works great.

    The correct code for the record:

    In the unit's implementation::

    msSaveReport : TMemoryStream ;


    In the unit's formcreate::

    msSaveReport := TMemoryStream.create ;
    MyReport.template.saveToStream(msSaveReport) ;


    In the unit's formclose:

    msSaveReport.free ;


    In the buttonclick method to run the report:

    MyReport.template.new ;
    msSaveReport.position := 0 ;
    MyReport.template.loadFromStream(msSaveReport) ;
    // code to modify and run report follows...
This discussion has been closed.