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

Using Variables with Formulas

The following topics are discussed in this section.

Introduction, Page 215

Examples, Page 216

Introduction

Delphi variables can be used with Crystal Reports Formulas and the Crystal Component's Formulas object if they are declared as string or converted to string (regardless of the actual data type) before being passed to the Report. The value of the variable passed to the Report must be in the same format and look exactly as if entered directly into the Crystal Reports Formula Editor:

String data types must be surrounded by double quotes: "This is a String". Single quotes are also valid within Crystal Reports but because Delphi uses single quotes, all string data types passed to Crystal should use double quotes to avoid conflicts.

Numbers do not need quotes: 1234

Dates must be put in the Crystal Reports Date function format: Date(YYYY,MM,DD)

The VCL Formulas object can only be used to change formulas that already exist in Crystal Reports, not to create new formulas. The Formulas object can contain numerous Formula items, which can either be created by using the Retrieve method:

Crpe1.Formulas.Retrieve;

or the manual Add method:

Crpe1.Formulas.Add('FormulaOne');

NOTE: Although Crystal Reports automatically prefixes the @ symbol to each Formula Name when a Formula is created, the @ must not be included when using the Name or Add methods of the Formulas object in Delphi.

Once the Formulas object has items in it, the next step is to navigate to the desired item. This can be done in 3 different ways:

1.Use the default Item array property (subscript):

Crpe1.Formulas[0];

2.Use the Name lookup property:

Crpe1.Formulas.Name := 'FormulaOne';

3.Use the ItemIndex property:

Crpe1.Formulas.ItemIndex := 0;

Seagate Crystal Visual Component Library

215

Once the desired Formula has been located, the Formula property can be set. This is a stringlist that contains the actual Formula text. Since it is a stringlist, three different methods can be used to write to it:

1.The default Strings array (subscript):

{Clear the Formula first} Crpe1.Formulas.Formula.Clear; Crpe1.Formulas.Formula[0] := '"Formula String"';

NOTE: It is also acceptable, and perhaps easier to read, if the subscript specifying the Formulas item is also used in the expression, although since the VCL objects have an internal Index, it is not strictly necessary:

{Clear the Formula first} Crpe1.Formulas[0].Formula.Clear; Crpe1.Formulas[0].Formula[0] := '"Formula String"';

If the subscript value is not known, it can be obtained right after setting the Formula Name:

{Clear the Formula first} Crpe1.Formulas.Name := 'FormulaOne'; i := Crpe1.Formulas.ItemIndex;

Crpe1.Formulas[i].Formula.Clear;Crpe1.Formulas[i].Formula[0] := '"Formula String"';

2.The Text property:

Crpe1.Formulas.Formula.Text := '23';

3.The Assign method

Crpe1.Formulas.Formula.Assign('Date(1998,01,01)');

Examples

The following examples are presented in this section.

Passing a variable from Delphi that results in a String data type in Crystal Reports, Page 216 Passing a variable from Delphi that results in a Numeric data type in Crystal Reports, Page 218 Passing a variable from Delphi that results in a Date data type in Crystal Reports, Page 219

Passing a variable from Delphi that results in a String data type in Crystal Reports

As both Delphi and Crystal Reports require string values to be surrounded by quotes, a variable that results in a string data type in Crystal Reports requires two sets of quotes: one set for Delphi and one set for Crystal Reports. The first two examples show how to pass the variable when the value of the variable is hard-coded. The third example shows how to pass the variable when the value of the variable is entered via an EditBox at runtime.

Seagate Crystal Visual Component Library

216

Example 1: The value is hard-coded and includes the extra quotes while assigning the variable a value:

var

sTmp: string; {Declare sTmp as a string variable} begin

{Assign a value to the variable. Note the use of two sets of quotes} sTmp := '"This is a string"';

Crpe1.Formulas.Name := 'StringFormula';

{Set the variable to the VCL Formula property}

Crpe1.Formulas.Formula.Text := sTmp;

{Display the value that is passed to the Report}

ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: "This is a string"} {The Report will print: This is a string}

Example 2: The value is hard-coded and includes the extra quotes in the Formula statement:

var

sTmp: string; {Declare sTmp as a string variable} begin

{Assign a value to the variable. Note the use of one set of quotes} sTmp := 'This is a string';

Crpe1.Formulas.Name := 'StringFormula';

{Concatenate Formula with: opening quote, variable name, closing quote} Crpe1.Formulas.Formula.Text := '"' + sTmp + '"';

{Display the value that is passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: "This is a string"} {The Report will print: This is a string}

Example 3: Shows how to pass a variable when the value is entered via an EditBox at runtime:

var

sTmp: string; {Declare sTmp as a string variable} begin

{Assign the contents of an EditBox to the sTmp variable} sTmp := Edit1.Text;

Crpe1.Formulas.Name := 'StringFormula';

{Concatenate Formula with: open quote, variable name, close quote}

Crpe1.Formulas.Formula.Text := '"' + sTmp + '"'; {Display the value passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: EditBox value with surrounding double quotes} {The Report will print: EditBox value without surrounding double quotes}

Seagate Crystal Visual Component Library

217

Passing a variable from Delphi that results in a Numeric data type in Crystal Reports

Because the Formulas object passes only string data types, variables declared as type Integer must be converted to a string before being passed to Crystal Reports. The first example shows a number in a variable declared as type String. The second example shows a variable declared as type Integer converted to a string by using an additional string variable and the Str() function.

Example 1: The value is hard-coded and the variable declared as type String:

var

sNum: string; {Declare sNum as a string variable} begin

{Assign a value to the variable. Note the use of one set of quotes} sNum := '1234';

Crpe1.Formulas.Name := 'NumberFormula';

{Pass the variable value to the Formula text} Crpe1.Formulas.Formula.Text := sNum;

{Display the value passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text); {The Message Box will display: 1234} {The Report will print: 1234}

Example 2: The value is hard-coded and the variable declared as type Integer:

var

nTmp: integer; {Declare nTmp as an integer variable} sNum: string; {Declare sNum as string variable}

begin

{Assign a value to the integer variable.} nTmp := 1234;

{Assign the value of nTmp to the sNum string variable} Str(sNum, nTmp;

Crpe1.Formulas.Name := 'NumberFormula';

{Pass the variable value to the Formula text}

Crpe1.Formulas.Formula.Text := sNum;

{Display the value passed to the Report}

ShowMessage(Crpe1.Formulas.Formula.Text); {The Message Box will display: 1234} {The Report will print: 1234}

Seagate Crystal Visual Component Library

218

Passing a variable from Delphi that results in a Date data type in Crystal Reports

Crystal Reports accepts dates only as a string data type in the Date(yyyy,mm,dd) format. The first example shows the date hard-coded and assigned to a single variable as a numeric string. The second example shows the date hard-coded and assigned to three variables as numeric strings. The third example shows how to pass the variable when the date is entered via three EditBoxes at runtime.

Example 1: The date is hard-coded and assigned to a single variable as a string. Formatting is done in the Formula statement:

var

sDate: string; {Declare sDate as a string variable} begin

{Assign a value to the variable. Note that the year is 4 digits, the month and day are 2 digits. The year, month and day are in the order Crystal Reports expects and are delimited by commas, and the entire string is surrounded by quotes.}

sDate := '1995,01,31'; Crpe1.Formulas.Name := 'DateFormula';

{Concatenate Formula with: the word Date, opening parenthesis, the variable name, closing parenthesis}

Crpe1.Formulas.Formula.Text := 'Date(' + sDate + ')'; {Display the value passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: Date(1995,01,31)} {The Report will print: 95/1/31}

{The Date format will be Windows default unless formatted otherwise in the Report}

Example 2: The date is hard-coded and assigned to three variables as numeric strings. Formatting is done in the Formula statement:

var

{Declare sYear, sMonth, sDay as string variables} sYear, sMonth, sDay: string;

begin

{Assign a value to sYear; Note that the year is 4 digits}

sYear := '1995';

{Assign a value to sMonth; Note that the month is 2 digits} sMonth := '01';

{Assign a value to sDay; Note that the day is 2 digits} sDay := '31';

Crpe1.Formulas.Name := 'DateFormula';

{Concatenate the Formula with: the word Date, opening parenthesis, the sYear variable, a comma, the sMonth variable, a comma, the sDay variable,

a comma, and a closing parenthesis}

Seagate Crystal Visual Component Library

219

Crpe1.Formulas.Formula.Text := 'Date(' + sYear + ',' + sMonth + ',' + sDay + ')';

{Display the value passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: Date(1995,01,31)} {The Report will print: 95/1/31}

{The Date format will be Windows default unless formatted otherwise in the report}

Example 3: The date is entered via three EditBoxes at runtime. The formatting is included while assigning the variable its value:

var

{Declare sYear, sMonth, sDay, and sWholeDate as string variables} sYear, sMonth, sDay, sWholeDate: string;

begin

{Assign the Year Entry EditBox value to sYear} sYear := Edit1.Text;

{Assign the Month Entry EditBox value to sMonth} sMonth := Edit2.Text;

{Assign the Day Entry EditBox value to sDay} sDay := Edit3.Text;

{Concatenate Formula with: the word Date, an opening parenthesis, the sYear variable, a comma, the sMonth variable, a comma, the sDay variable, a

comma, and a closing parenthesis, and assign the value to the sWholeDate variable}

sWholeDate := 'Date(' + sYear + ',' + sMonth + ',' + sDay + ')'; Crpe1.Formulas.Name := 'DateFormula';

{Pass the variable value to the Formula text} Crpe1.Formulas.Formula.Text := sWholeDate; {Display the value passed to the Report} ShowMessage(Crpe1.Formulas.Formula.Text);

{The Message Box will display: Date(YYYY,MM,DD) where YYYYMMDD represents whatever was entered in the Year Entry, Month Entry, and Day Entry Edit Boxes at runtime}

{The Report will print: YYYY/MM/DD}

{The Date format will be Windows default unless formatted otherwise in the Report}

Seagate Crystal Visual Component Library

220

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