Home General

Close report preview from code

I am running a series of reports, monthly reports with the as of date changing. (When the data is obtained and calculated for each report date, I save it in a historical database, which is outside of RB). Since the same data calcs are used to display reports that go into the history DB, it is very handy to do both at the same time (looping the as of date).
This works very well, but is inconvenient for the user to have to close each report before the next report is run. I have tried closing the report using the "Cancel" event in the AfterPrint event and a few other places, but the previewed report sits on the screen until the user closes it, then proceeds to the next report in the loop. I am thinking if I print the report to a file, that may work, but it is nice for the user to see the report even briefly on the screen as an assurance the monthly reports and calcs were done.

Scott S.

Comments

  • Hi Scott,

    If you are just previewing, try using the Viewer.OnEndGenerate to determine when the report has been fully generated. Then make a call to Report.PreviewForm.Close. If you are exporting to a file or printing, the AfterPrint event should work with the same code. Something like the following...

    ...
    private
    procedure eh_EndGenerate(aSender: TObject);

    public
    { Public declarations }
    ...
    implementation

    uses
    ppViewr;

    ...

    procedure TForm1.eh_EndGenerate(aSender: TObject);
    begin
    Sleep(1000);

    ppReport2.PreviewForm.Close;

    end;

    procedure TForm1.ppReport2PreviewFormCreate(Sender: TObject);
    begin
    TppViewer(ppReport2.PreviewForm.Viewer).OnEndGenerate := eh_EndGenerate;

    end;

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thanks, almost works for me! I am missing something basic...

    I get to the line to close the preview form, but then then the loop to re-print the report hangs up after that. Originally I was calling ppReport2.Print, with the output to Screen, and have the AfterPrint event sending summed up data to the database. Without automatic closing, the user has to close the form manually and the loop re-runs the report with new as of date.

    I don't see that I could call ppRepport2.Preview if that was one of your options. Since I would just as soon call ppReport2.Print, with its AfterPrint event anyway, I tried your code:

    Sleep(1000);
    ppReport2.PreviewForm.Close;

    at the end of the AfterPrint event, but that did not work either?

    Scott
  • Hi Scott,

    I sent you an example of this working. I am not getting the "hanging" that you are for my example.

    There is no such call as Report.Prevew. This would be Report.Print with the DeviceType equal to Screen.

    When previewing, the AfterPrint event fires once the preview form is closed so trying to close the form in that event will not work.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thanks much, the example works great, and I sent an email back. The example has blank if any reports showing as they process through smoothly, but I expect they will display temporarily (ideal in my app for the user to briefly see they are being processed), in my real world project when there is connected data.
Sign In or Register to comment.