Dynamically Changing/Setting Dataset
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:
...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.
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
- 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.