Modifying the variable’s value
There are two ways to modify the value of a variable:
Pascal:
frxReport1.Variables['My Variable 2'] := 10;
C++:
frxReport1->Variables->Variables["My Variable 2"] = 10;
or
Pascal:
var
Index: Integer;
Variable: TfrxVariable;
{ search for the variable }
Index := frxReport1.Variables.IndexOf('My Variable 2');
{ if it is found, change a value }
if Index <> -1 then
begin
Variable := frxReport1.Variables.Items[Index];
Variable.Value := 10;
end;
C++:
int Index;
TfrxVariable * Variable;
// search for the variable
Index = frxReport1->Variables->IndexOf("My Variable 2");
// if it is found, change a value
if(Index != -1)
{
Variable = frxReport1->Variables->Items[Index];
Variable->Value = 10;
}
It should be noted, that when accessing a report variable its value is calculated if it is of string type. That means the variable which value is 'Table1."Field1"' will return a value of a DB field, but not the 'Table1."Field1"' string. You should be careful when assigning a string-type values to report variables. For example, the next code will raise exception "unknown variable 'test'" when running a report:
frxReport1.Variables['My Variable'] := 'test';
because FastReport trying to calculate a value of such variable. The right way to pass a string values is:
frxReport1.Variables['My Variable'] := '''' + 'test' + '''';
In this case the variable value - string 'test' will be shown without errors. But keep in mind that:
string should not contain single quotes. All single quotes must be doubled;
string should not contain #13#10 symbols.
In some cases it is easier to pass variables using a script.