How To…Launch the Report Wizard in Code

Question

“How can I launch the Report wizard directly in code?”

Solution

a. Simplest solution is to use the Designer

 uses
  ppWizard;

begin
   // note: this example works with RB 10 and later 
   myDesigner.Form.LayoutManager.DocumentController.New(ppDefaultReportWizard);

end;

b. It is also possible to launch the report wizard without the Designer

Start by manually creating a TppReportWizard object and implementing the necessary events.

– OnCreateReport: Create a report object and assign a datapipeline.

– OnGetDataNames: Assign the field names from the pipeline to the DataNameList.

– OnGetFields: Add the datapipeline fields to the field list.

– OnValidateName: Set the aNameValid parameter to True.

Next execute the report wizard to show the dialog.

Download: LaunchReportWizardInCode.zip

Sample Delphi code:

uses
  ppRptWiz;

procedure TForm1.btnLaunchWizardClick(Sender: TObject);
var
  lWizard: TppReportWizard;
begin

  lWizard := TppReportWizard.Create(Self);
  lWizard.OnCreateReport := CreateReportEvent;
  lWizard.OnGetDataNames := GetDataNamesEvent;
  lWizard.OnGetFields := GetFieldsEvent;
  lWizard.OnValidateName := ValidateNameEvent;

  FReport := nil;

  if (lWizard.Execute) then
    FReport.Print;

  lWizard.Free;

  FReport.Free;
end;

procedure TForm1.CreateReportEvent(Sender: TObject; const aDataName: String; var aReport: TObject);
begin
  FReport := TppReport.Create(Self);

  FReport.DataPipeline := DataPipelineForName(aDataName);

  aReport := FReport;

end;

procedure TForm1.GetDataNamesEvent(Sender: TObject; aDataNameList: TStrings);
begin
  aDataNameList.Assign(FDataPipelines);

end;

procedure TForm1.GetFieldsEvent(Sender: TObject; const aDataName: String; aFieldList: TStrings);
var
  lDataPipeline: TppDataPipeline;
  liIndex: Integer;
begin

  lDataPipeline := DataPipelineForName(aDataName);

  if (lDataPipeline <> nil) then
    for liIndex := 0 to lDataPipeline.FieldCount - 1 do
      aFieldList.AddObject(lDataPipeline.Fields[liIndex].FieldAlias, lDataPipeline.Fields[liIndex]);

end;

procedure TForm1.ValidateNameEvent(Sender: TObject; const aName: String; var aNameValid: Boolean);
begin
  aNameValid := True;
end;

function TForm1.DataPipelineForName(const aDataName: String): TppDataPipeline;
var
  liIndex: Integer;
begin
  liIndex := FDataPipelines.IndexOf(aDataName);

  if (liIndex <> -1) then
    Result := TppDataPipeline(FDataPipelines.Objects[liIndex])
  else
    Result := nil;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FDataPipelines := TStringList.Create;

  FDataPipelines.AddObject('Customers', plCustomer);
  FDataPipelines.AddObject('Orders', plOrder);
  FDataPipelines.AddObject('Sea Fish', plSeaFish);

end;