Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
172
Добавлен:
11.05.2015
Размер:
4.73 Mб
Скачать

FieldMapping Example

If the database structure or field names have changed, the following code will bring up Crystal's built-in Field Mapping dialog box:

Crpe1.ReportName := 'C:\Company.rpt';

Crpe1.FieldMapping := fmPrompt;

Crpe1.VerifyDatabase;

Formulas

Declaration

property Formulas: TCrpeFormulas;

Type

TCrpeFormulas = class(TPersistent)

Description

The Formulas object contains all the properties that apply to Formulas in a Report.

The Name property specifies the name of the formula.

The Formula property holds the formula text.

The default array property Item, and the ItemIndex property, can be used to navigate through the various formulas that may be stored in the Formulas object.

Formulas may be retrieved from the Report via the Retrieve method.

The Add/Delete methods can be used instead of Retrieve/Clear to manually add and remove items in the Formulas object.

The Check method provides an easy way of checking the syntax of the Formula text before running the Report.

Formulas are a very powerful feature in Crystal Reports and besides their normal summary functions, can be used at runtime to control everything from Section and Field formatting to Grouping and Sorting.

Formulas Example

The following procedures show how easy it is to use the features of the new VCL in the Delphi environment. The components placed on the Form are:

TListBox - lbFormulaNames

TMemo - memoFormulas

TButton - FormulaCheck

VCL Reference

27

The Button click event is associated with this procedure:

procedure btnFormulaCheckClick(Sender: TObject);

The ListBox click event is associated with this procedure:

procedure lbFormulaNamesClick(Sender: TObject);

The Memo change event is associated with this procedure:

procedure memoFormulasChange(Sender: TObject);

This procedure initializes the Form's components with data from the Report:

procedure UpdateForm;

The Formula names are loaded into the ListBox in the UpdateForm procedure. When a name is clicked in the ListBox, the Memo component becomes updated with the Formula Text associated with that formula name. When the formula is edited in the memo, it is written back to the Crystal component's Formula Text property. Finally, when the button is clicked, the Formula is checked to see if the syntax is good.

{UpdateForm procedure} procedure TfrmMain.UpdateForm; var

cnt: smallint; begin

lbFormulaNames.Clear; Crpe1.ReportName := 'C:\Company.rpt'; Crpe1.Formulas.Retrieve;

{Loop through Formulas and add Formula Names to ListBox} if Crpe1.Formulas.Count > 0 then

begin

for cnt := 0 to (Crpe1.Formulas.Count - 1) do begin

Crpe1.Formulas[cnt];

lbFormulaNames.Items.Add(Crpe1.Formulas.Name); end;

lbFormulaNames.ItemIndex := 0; lbFormulaNamesClick(Self);

end; end;

{lbFormulaNamesClick procedure}

{Notice how we disable the Memo OnChange event. If we don't do this, it will write back the first line to the VCL, overwriting the rest of the formula stored there}

VCL Reference

28

procedure TfrmMain.lbFormulaNamesClick(Sender: TObject); begin

Crpe1.Formulas[lbFormulaNames.ItemIndex]; {Disable the OnChange event.} memoFormulas.OnChange := nil;

{Copy the Formula from the VCL} memoFormulas.Lines.Assign(Crpe1.Formulas.Formula); {Re-enable the OnChange event} memoFormulas.OnChange := memoFormulasChange;

end;

{memoFormulasChange procedure}

procedure TfrmMain.memoFormulasChange(Sender: TObject); begin

Crpe1.Formulas.Formula.Assign(memoFormulas.Lines); end;

{btnFormulaCheckClick procedure}

procedure TfrmMain.btnFormulaCheckClick(Sender: TObject); begin

if Crpe1.Formulas.Check then ShowMessage('Formula is good')

else

ShowMessage('Formula has an error');

end;

Formulas Properties

Formulas Formula property, Page 290

Formulas Item property, Page 291

Formulas ItemIndex property, Page 292

Formulas Name property, Page 293

Formulas Methods

Formulas Add method, Page 293

Formulas Check method, Page 294

Formulas Clear method, Page 295

Formulas CopyFrom method, Page 296

Formulas Count method, Page 296

Formulas Create method, Page 297

VCL Reference

29

Соседние файлы в папке crystal