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

Report explorer and TppReport AfterPrint

edited June 2004 in General
Dear All,

I would like to do some processing when an end user prints from report
explorer. I'm having trouble getting the AfterPrint method to fire.

I've tried attaching the AfterPrint method of the report attached to the
designer, but that doesn't fire. I've created a separate report and
attached it to the report explorer using the SetReport method. The
AfterPrint method doesn't fire. I've tried attaching the AfterPrint events
at run time following the display of the report explorer. Still no joy.
Having trawled through the report builder source code, I can see that the
report explorer has its own internal report, FReport. This is the one
that's used to print / preview the report. This does not have any events
attached to it. FReport is a private member of the report explorer class
and I have no access to it externally. I'm not keen on hacking the source
code to attach events.

What would be the best way to get an event to fire when a user prints any
report from report explorer?

Regards Giles.

Comments

  • edited June 2004
    Hi Giles,

    The reason these events are not firing is that you are loading templates
    into your report object and once this is done, all event handler references
    are lost. The best way to work around this issue is to keep the event
    handler code local to the report template using RAP. See the article below
    for more information on lost event handlers.

    --------------------------------------------
    Article: Troubleshooting Lost Event Handlers
    --------------------------------------------

    Let's assume you have created a report in Delphi and assign an event
    handlers to the OnPreviewFormCreate event of the report. The event is
    generated by Delphi as:

    procedure TForm1.ppReport1PreviewFormCreate(Sender: TObject);

    You then save the report to an RTM file 'Report1.RTM.' The events are
    stored as references only, and so the RTM contains:

    object ppReport1: TppReport
    .
    .
    OnPreviewFormCreate = ppReport1PreviewFormCreate
    end

    You then go on to work on a different report. Saving it with under then
    name 'Report2.RTM'. Only this time, before you save the report you
    change the report component name to: rptOrders. Delphi automatically
    updates the event declaration for OnPreviewFormCreate event to:

    procedure TForm1.rptOrdersPreviewFormCreate(Sender: TObject);


    You then create two buttons on the form, one to load Report1 and
    preview, the other to load Report2 and preview. When you run the app
    and click Report1, you an error. This is because the Report1.RTM file
    contains a reference to ppReport1PreviewFormCreate, a method which no
    longer exists (at least with this name) in the form.

    One answer is to load all your rtm files into the report component you
    will be using for loading. Fix any errors, reassign any events that get
    cleared. This will update your rtms to contain the proper event handler
    names.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited June 2004
    Hello Nico,

    Thanks for the prompt reply. A couple of questions:


    references
    below

    When you say keep the event handler code local to the report template, does
    this mean I have to add the code to every single report? If so, I'm not
    sure this would be practicable, as our system now has over 100 standard
    reports and our customers modify / design their own reports using the
    designer.



    I did search through the news group and found your trouble shooting lost
    event handlers reply to a previous post. I didn't understand how to
    implement the last paragraph. When the user has loaded a report in report
    explorer, the internal FReport is used. How do I get access to FReport to
    fix up the event handlers at run time?


    Regards Giles.
  • edited June 2004
    Hi Giles,

    You will need to use the template event OnLoadEnd to get access to the
    report once the template has loaded. Here is another article that may be
    helpful.

    ----------------------------------------------
    Tech Tip: Using Template Events
    ----------------------------------------------

    The Report.Template object has several events that can be used for
    customizing what happens when a report is loaded or saved:

    - OnLoadStart
    - OnLoadEnd
    - OnNew
    - OnSaveStart
    - OnSaveEnd


    The OnLoadEnd and OnNew events are often used to perform actions related
    to report and data initialization.

    The OnSaveEnd event is often used to save additional descriptive
    ("meta") data to the database each time the report is saved.

    Example:

    The Report.Template events are public and therefore must be assigned at
    run-time.


    1. In the private section of your form declaration you can declare an
    event-handler method:

    TForm = class(TForm)
    private
    procedure myTemplateOnLoadEndEvent(Sender: TObject);

    public

    end;


    2. In the Form.OnCreate event, you can assign the event-handler to the
    event:

    procedure TForm1.FormCreate(Sender: TObject);
    begin

    ppReport1.Template.OnLoadEnd := myTemplateOnLoadEndEvent;

    end;


    3. Implement the event-handler method:

    procedure TForm1.myTemplateOnLoadEndEvent(Sender: TObject);
    begin

    {add code here to initial the report or data, etc. }
    ppReport1.PrinterSetup.MarginTop := 0.5;

    end;

    --
    Best Regards,

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