Calling the Variable from the Report's Variable Li
Calling the Variable from the Report's Variable List
Any variable that is defined in the list of the report variables (“Report>Variables...” menu item) can be referenced in a script. The variable’s name should be enclosed in angle brackets:
if <my variable> = 10 then ...
An alternative way is to use the “Get” function:
if Get('my variable') = 10 then ...
A variable’s value is changed only via the “Set” procedure:
Set('my variable', 10);
It is worth noting that to assign a string value to the variable you must add quotes around the value:
Set('my variable', '''' + 'String' + '''');
System variables, such as “Page#,” should be referenced in exactly the same way:
if <Page#> = 1 then ...
Referencing the Database Fields
Just as with variables, angle brackets should be used when referencing DB fields in a report:
if <Table1."Field1"> = Null then...
Alternatively the “Get” function can be used for accessing DB fields (in fact, this function is used implicitly by FastReport when calculating expressions enclosed in angle brackets).
Using Aggregate Functions in the Script
An idiosyncrasy of aggregate functions is that they must be used inside “Text” objects; once used in this manner they can then be used in the script itself. If an aggregate function only appears in a script (without appearing in a “Text” object) an error message is generated. This happens because an aggregate function must be connected to a specific band; once so connected it will work correctly.
Displaying a Variable's Value in a Report
Variables can be declared and used locally within a script. Once declared a script variable can have a value assigned to it. Here is a simple example of a script variable in use:
var
MyVariable: String;
begin
MyVariable := 'Hello!';
end.
The variable’s value can be displayed in a “Text” object, for example, by typing '[MyVariable]' into the object.
A variable’s name must be unique. This means the name must not duplicate the name of any other report object, standard function or constant. If there is an error in a script, a message will be displayed when the report is run and report construction will be stopped.