Is it possible to display Delphi variables in a report?

hsmhsm
edited 1:48PM in FastReport 4.0
I love FastReport - so much better than the reports in Access. However..

In my delphi code I use functions to generate dates that are then used as criteria in the initial query that feeds my report.

I've also stored them as strings in the same form/unit that contains the FastReport and have made functions to return these strings and declared them in the public part of the form definition containing the report. (Hope that makes sense!)

I'd like to display these strings on the report as headers, above the appropriate column, to show the criteria that was used.

I've played arpound with the using a text object to try to display the result of my delphi function using things like [Form2.getFirstDate] but get an undefined variable error all the time.

Can we use delphi variables or the result of delphi functions in our reports?
If so, how?

Howard

Comments

  • gordkgordk St.Catherines On. Canada.
    edited 1:48PM
    Not the way you are trying to do it.
    Read the programmers manual on working with categorized variables, and the ongetvalue event as well. you can set the values of categorized variables after a report is loaded and before calling
    show or prepare it can also be set from the obp event of the tfrxreport component.
    Hint when passing string values in to a categorized variable you must use extra string delimiters.
    or the variable when used, will appear to be an unknown expression an it will cause an error to be
    thrown unless a handler has been written for the ogv event
  • hsmhsm
    edited 1:48PM
    gordk wrote: »
    Not the way you are trying to do it.
    Read the programmers manual on working with categorized variables, and the ongetvalue event as well. you can set the values of categorized variables after a report is loaded and before calling
    show or prepare it can also be set from the obp event of the tfrxreport component.
    Hint when passing string values in to a categorized variable you must use extra string delimiters.
    or the variable when used, will appear to be an unknown expression an it will cause an error to be
    thrown unless a handler has been written for the ogv event

    Thank you
    I have defined my variables under a new category and can see them in the tree, (one of them is called DateOfWeekMinus6)
    My tfrxreport component is called frxReportLeagueTable, it is on Form2. This form also contains in the interface a global variable called strDateLastWeekMinus6.

    I have put the following in the frxReportLeagueTableOnReportPrint event
    frxReportLeagueTable.variables := '''' + (form2.strDateLastWeekMinus6) + '''';
    but I get an 'unknown identifer' error on frxReportLeagueTable (the report name).

    I also tried putting the same line in the BeforePrint event of a text object (Memo12OnBeforePrint) and also dragged the variable from the tree onto my report and put the same line in its BeforePrint event (DateOfWeekMinus6OnBeforePrint) each time getting an unknown identifer on frxReportLeagueTable.

    Any ideas?
    Howard
  • Anu de DeusAnu de Deus Hampshire, UK
    edited January 2011
    Yes, you can call a function defined in your Delphi unit.

    Put this before any call to Show or Prepare:

    frxReport1.OnUserFunction := CustomReportFunction;
    frxReport1.AddFunction('function SetDocImgRef(const s: string) :string;', 'MyFuncs'); << this is your function, declare it the way you would in a .pas unit, plus the group name (not relevant unless in the report designer)

    function TYourForm.CustomReportFunction(const MethodName: string; var Params: Variant): Variant;
    var
    lStr1: string;
    begin
    result := MethodName;
    if MethodName = 'SETDOCIMGREF' then << YOU MUST HAVE IT UPPERCASE IN THE COMPARISON HERE, or use CompareText() instead
    result := SetNewDocImgRef(Params[0]) << this is your function being called
    else
    (...)
    end;

    And this is your function:

    function TYourForm.SetNewDocImgRef(const s: string): string;
    begin
    result := 'Easy example '+s;
    end;


    TYourForm is your form or class where you are doing everything from, where you have your frxReport1 object.

    After that you can call the function SETDOCIMGREF from the frx Script or put in a memoview as the text property, like '[SETDOCIMGREF('test')]'

    If you didn't understand, search for OnUserFunction and AddFunction in the manuals
  • hsmhsm
    edited 1:48PM
    Yes, you can call a function defined in your Delphi unit.

    Put this before any call to Show or Prepare:

    frxReport1.OnUserFunction := CustomReportFunction;
    frxReport1.AddFunction('function SetDocImgRef(const s: string) :string;', 'MyFuncs'); << this is your function, declare it the way you would in a .pas unit, plus the group name (not relevant unless in the report designer)

    function TYourForm.CustomReportFunction(const MethodName: string; var Params: Variant): Variant;
    var
    lStr1: string;
    begin
    result := MethodName;
    if MethodName = 'SETDOCIMGREF' then << YOU MUST HAVE IT UPPERCASE IN THE COMPARISON HERE, or use CompareText() instead
    result := SetNewDocImgRef(Params[0]) << this is your function being called
    else
    (...)
    end;

    And this is your function:

    function TYourForm.SetNewDocImgRef(const s: string): string;
    begin
    result := 'Easy example '+s;
    end;


    TYourForm is your form or class where you are doing everything from, where you have your frxReport1 object.

    After that you can call the function SETDOCIMGREF from the frx Script or put in a memoview as the text property, like '[SETDOCIMGREF('test')]'

    If you didn't understand, search for OnUserFunction and AddFunction in the manuals


    Thank you I shall have a go (although it still does noes not explain whey I get an unknown unidentifier error by simply substituting into the manual's code the real name of my report instead of frxReport1) .

    Which manuals are you referring to? I've searched both the User Manual and the Programmers manual and neither of them have any reference to OnUserFunction - or any function come to that.

    Howard
  • Anu de DeusAnu de Deus Hampshire, UK
    edited 1:48PM
    The Developers Manual, page 43, section Custom Functions Connection to Report

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.