Delphi - How to get result of function from QuickReport without viewing a report?

71 Views Asked by At

In Delphi 7 I have a report written in QuickReport.

The report is called by the function from another form

     var RepResult : integer
     RepResult := ShowReport(param1,param2...);

The function inside report looks like

  `  var QuickRep1 : TQuickReport;
        ReportResult : integer;
     function ShowReport(param1,param2...) : integer
     begin
      ...
      QuickRep1.PreviewModal;
      Result := ReportResult;
    end;`

The calculation of variable ReportResult is complicated, based on many queries inside QuickReport and calculated in events of DetailBands, ChildBands ,LoopBands and so on . The report and the calculations works fine , but now the customer wants to see result of this calculations in another place without viewing the report. So the question is : Is it possible to get only the result of the function without previewing or printing the report ?

I tried QuickRep1.PreviewModeless and quickrep1.print to non-existed printer. Nothing works

1

There are 1 best solutions below

0
Ineffable21 On

Make your function public so you can call it everywhere in your code. After that you need to create a public boolean that would tell you whether to show report or not. It could look like this:

var
  shouldShowReport : boolean = false;

Then what you need to do is change your function a bit, by adding a boolean as a parameter like this:

function ShowReport(param1,param2...; showReport : boolean) : integer

Then you need to change your logic based on the passed boolean in a way that you can either show the report like now, or just return an integer without the previewModel. This is an example:

function ShowReport(param1,param2...; showReport : boolean) : integer
begin
  //If you need to show the report then you call the PreviewModal
  if showReport then begin
    ...
    QuickRep1.PreviewModal;
  end;
  
  //Then you get the result anyway
  Result := ReportResult;
end;

After that you need to figure out where you are going to call the function and change the value of shouldShowReport based on wether you need to show the report in that logic. And then pass the boolean as a parameter to the function when calling it:

//If this is the part where the customer doesn't want to see the whole report
shouldShowReport := false;
RepResult := ShowReport(param1,param2..., shouldShowReport);
//repResult is an integer like you've shown already

And finally you need to show the result somehow. I don't know how you plan to do it but the simpliest way is with a label. You get it from the components and place it on you form. After that you can change it up a bit with the properties based on what you want. Then you need to give the result to the label caption property. Don't forget that your result is an integer and if you directly pass an integer to a caption you'd get an error. That's why you need to make it a string first. For the example I would assume you would name your label lblResult

lblResult.Caption := IntToStr(RepResult);