Home Subreports
New Blog Posts: Merging Reports - Part 1 and Part 2

Dublicate subreport at runtime

edited May 2009 in Subreports
Hi everybody!

My english is bad, hope you can understand.

I have a Report and 15 subreports. The subreports must be printed when
field 'Zyklus' in Table corresponds with subreport.username.

It works, when one 'ZYKLUS'-content different from other. But if one
fieldvalue of 'Zyklus' the same like the previous - I get an
EInvalidPropertyError-exception because Subreport.ShiftRelativTo to the
same Subreport!

Sample:
var i,sub,max:integer;
FirstReport:Boolean;
Report,tempreport:TppSubReport;
FileName:String;

begin
TDBTable.open;
max:=TDBTable.RecordCount;
TDBTable.First;
FirstReport:=False;
For i:=1 to max do
begin
IF TDBTable.FieldValues['PROGRAMID']=programid Then
For sub:=1 to 15 do
begin
Report:=TppSubReport(FindComponent('ppsubReport'+intToStr(sub)));
IF Report.UserName =TDBTable.FieldValues['ZYKLUS'] Then
begin
FileName:=LowerCase(Report.UserName)+'.rtm';
Report.Report.Template.FileName := FileName;
report.Report.Template.LoadFromFile;
If FirstReport=False Then Firstreport:=True
else Report.ShiftRelativeTo:=TempReport; //--> Exception

Report.visible:=True;
Tempreport:=Report;
end;
end;
TDBTable.Next;
end;
end;

What can I do?

Thanks!
Juergen Fischer
j.fischer-steuerungstechnik.de

Comments

  • edited May 2009
    Hi Juergen,

    What would you like to happen if the 'ZYKLUS' field returns the same record?
    You could easily skip that record by placing a condition after you retrieve
    the subreport object to be sure the "report" is not the same as the
    "tempreport". Something like the following...

    IF (Report.UserName =TDBTable.FieldValues['ZYKLUS']) and (Report.UserName <>
    TempReport.UserName) Then

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited May 2009
    Hi Nico,

    The 'ZYKLUS'-Field returns not the same record. It returns another
    record with the same content in Field 'ZYKLUS'. Therefore it must be
    printed and i can't skip!


    Zyklus Field2 Field3 Field4 ....
    name1 1 2 3
    name2 3 1 3
    name3 1 3 2
    name3 2 3 1
    name1 1 2 3

    Best regards

    Juergen


    Nico Cizik (Digital Metaphors) schrieb:
  • edited May 2009
    Hi Juergen,

    This is not necessarily a ReportBuilder issue however I will step through
    your code with you.

    The problem is your "for" loop logic. Since you are stepping through every
    subreport for each record based on a field that is not unique you are going
    to get conflicts. For instance, ignoring the error you get with "name3", if
    the data for Field2, Field3, etc for both "name1" record sets were
    different, both subreports would show the second set of data.

    The best solution would be to make sure every subreport had a unique
    UserName and to use a key field to define them. If your data does not allow
    this I belive the next best solution would be to keep track of which
    subreports have had templates successfully loaded as you move through your
    dataset. You could for instance, add the subreport's Name property to a
    string list once it has been loaded, then before loading it again, be sure
    the name is not in the list.

    ReportList := TStringList.Create;
    ...

    For sub:=1 to 15 do
    begin
    Report:=TppSubReport(FindComponent('ppsubReport'+intToStr(sub)));
    IF (Report.UserName =TDBTable.FieldValues['ZYKLUS']) and
    (ReportList.IndexOf(Report.Name) = -1) Then
    begin

    ReportList.Add(Report.Name);

    FileName:=LowerCase(Report.UserName)+'.rtm';
    Report.Report.Template.FileName := FileName;
    report.Report.Template.LoadFromFile;
    If FirstReport=False Then Firstreport:=True
    else Report.ShiftRelativeTo:=TempReport; //--> Exception

    Report.visible:=True;
    Tempreport:=Report;
    end;
    end;


    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited May 2009
    Thanks!

    Nico Cizik (Digital Metaphors) schrieb:
This discussion has been closed.