How to use a function outside FR in a report
I want use a function in another unit of my project (or in the unit of the formular with frReport) in a script of a report (OnBeforePrint of a band). How i do it?
Regards, Ingo
[FR 2.51]
Regards, Ingo
[FR 2.51]
Comments
Since Fr is limited to the functions available in the expression editor
a userfunction is provided to work with external library functions.
the on user function of fast report
This function can pass up to 3 paramaters out of the report and return
1 variant value. When Fr encounters an uknown function name in it's script
it fires the onuserfunction event.
sample use would be myvarname :=[myfunctioname(p1,p2,p3)];
example of report script in an on before print event of a band.
myvarname :=[MONEYTOSTR([varaible/fieldname])];
clicking on the onuserfunction event of the Tfrreport component will
bring up delphi's code editor.
procedure TForm1.frReport1UserFunction(const Name: String; p1, p2, p3: Variant; var Val: Variant);
begin
end;
First we must declare variables of the correct type to hold the values
which will be passed out in the parameters
p1,p2,p3.
our code now looks like this. for this example we will only pass one paramater
procedure TForm1.frReport1UserFunction(const Name: String; p1, p2, p3: Variant; var Val: Variant);
var
d1: Double;
d2 : what ever type required; // if passing more parameters
d3 : what ever type required;
begin
if name <> 'MONEYTOSTR' then Exit; //if name does not match get out
d1 := frParser.Calc(p1); // get value from report and store in variable "d1"
val := MyMoneyToStr(d1); // modify the value in d1 with an external Delphi library function
// and return it to the report in value.
end;
Now myvarname in the report will contain the textstring.
So any time any report, handled by frreport1 encounters the
function MONEYTOSTR([varaible/fieldname]); internally
it will jump out to Delphi and do what is required.
a little better way to write this would be
begin
if AnsiCompareText(Name, 'MONEYTOSTR') = 0 then
begin
d1 := frparser.calc(p1);
val := MyMoneyToStr(d1);
end;
//....... place as many other functions here as needed.
end;
if you don't want this function available then before you load a report
into the report component set the onuserfunction to nil.
regards
it works.
I had seen, that the function not works, when it is a function without parameters, e.g.
function Checkit: boolean;
begin
// check the value
end;
and OnbeforePrint:
....
if AnsiCompareText('CheckIt', Name) = 0 then
begin
val:= CheckIt;
end;
FR fires a Exception 'unknown symbol CHECKIT'
It is correct?
Regards, Ingo