Home Subreports

Loading a subreport dynamically

edited February 2003 in Subreports
Maybe there is some help for my problem:

Below you can find the source for a Subreport-Component. At designtime you
assign the name of another report to the subreport and - at runtime - the
other report is loaded into the subreport. Useful for common headers and
footers.

The loading succeeds, but when the method StartOfmainReport returns I get an
exception. No idea why.

Any help is appreceated!

Bernd

type
TBafSubReport = class(TppSubReport)
private
FAutomatedLoad : Boolean;
FTemplateName : String;
procedure LoadSubreport;
protected
function GetTemplateName : string;
procedure SetTemplateName(Value: string);
function GetCaption : string; override;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure Generate; override;
procedure StartOfMainReport; override;
published
property AutomatedLoad : Boolean read FAutomatedLoad write
FAutomatedLoad default True;
property TemplateName : string read GetTemplateName write
SetTemplateName;
end;

implementation

{$R SubreportSOF.res}

// -- Create
constructor TBafSubReport.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FAutomatedLoad:=True;
DefaultPropName:='TemplateName';
DefaultPropEditType:=etEdit;
end;

// -- Destroy
destructor TBafSubReport.Destroy;
begin
inherited Destroy;
end;

// -- Generate
procedure TBafSubReport.Generate;
begin
Report.AutoStop:=True;
inherited Generate;
end;

// -- LoadSubreport
procedure TBafSubReport.LoadSubreport;
begin

Report.Template.FileName:=ExtractFileDir(Report.MainReport.Template.FileName
)+'\'+FTemplateName;
Report.Template.LoadFromFile; (* <<<<-------------------------- Succeeds
*)

Report.PrinterSetup.DocumentName:=Band.Report.MainReport.PrinterSetup.Docume
ntName;
end;

// -- StartOfMainReport
procedure TBafSubReport.StartOfMainReport;
begin
inherited StartOfMainReport;
if (AutomatedLoad) and not (Overflow) then begin
LoadSubreport
end;
end; (* <<<--------------------crash *)

// -- SetTemplateName
procedure TBafSubReport.SetTemplateName(Value: string);
begin
if FTemplateName<>Value then begin
FTemplateName:=Value
end;
end;

// -- GetTemplateName
function TBafSubReport.GetTemplateName : string;
begin
Result:=FTemplateName;
end;

// -- GetCaption
function TBafSubReport.GetCaption : string;
begin
Result:='Runtime template';
end;

initialization
ppRegisterComponent(TBafSubReport, 'Demo Components', 0, 0, 'Runtime
template', 0);

finalization
ppUnRegisterComponent(TBafSubReport);

end.

Comments

  • edited February 2003
    Ah, I should have read the NG and should have had looked at the demos.
    Everything there. Sorry for wasting time.

    Ths solutionis, to call

    Report.Engine.State := Report.Engine.State - [esInitialized];
    Init;

    After loading the new subreport.

    Cheers
    Bernd

This discussion has been closed.