Dynamically Changing/Setting Dataset

edited May 2017 in FastReport VCL 5
I'm working on a report that pulls data from multiple datasets, and in one section of it I'd like to be able to choose which dataset is used dynamically.

In more detail, I have two tables ("Table1" and "Table2") which share a lot of field names in common, and in some circumstances the report would show (say) [Table1."Value"], in others [Table2."Value"]. (The actual section contains several dozen accesses to different fields, all present in both tables.)

To implement that I thought I'd try putting the section in a subreport, then:
- Set the subreport's dataset in script prior to displaying it.
- In the subreport set the field references so that the table name is taken from a variable (e.g. [<TableName>."Value"]), and set that variable in script ahead of calling the subreport.

I've got the first of these working with script of the form:
SubReport.DataSet := Report.GetDataset('Table1') ;

...but am struggling with the second; no combination of "field with table name as variable" syntaxes, or variable settings, have worked - they all generate errors on report display.

Has anyone found a working version of this, or if nothing else can anyone confirm whether I'm trying the impossible?

Thanks in advance for all replies.

Comments

  • gpigpi
    edited 8:37PM
    I think you should pass through all report's objects and replace all expressions
  • edited 8:37PM
    gpi wrote: »
    I think you should pass through all report's objects and replace all expressions
    Thanks, although I have to admit I'm not 100% sure exactly what's meant there. Your suggestion did point me in a direction that's proved to be workable, though (and may well be what you had in mind!):

    - In the OnBeforePrint of the section containing the subreport, set a global variable to reference the dataset to be used (dsSource := Report.GetDataset('Table1'))
    - In the OnBeforePrint of the various sections in the subreport, set variables from this to hold the values of required fields (for each value, a line similar to:
    Set('LocalValue', dsSource.Value))
    - In the section itself, display the variable's value ([LocalValue])


    That may not be the most efficient way of doing things (I'm not yet very familiar with the detailed options of FastReports), but it does seem to work.

Leave a Comment