The information in this article applies to:
- Microsoft Visual FoxPro for Windows, version 3.0
- Microsoft FoxPro for Windows, versions 2.5, 2.5a, and 2.5b
- Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, and 2.5b
SUMMARY
You can use a report variable in conjunction with the Immediate IF
function (IIF) to count or sum a specific report field.
MORE INFORMATION
The IIF function returns one of two values, depending on the value of
a logical expression. If the logical expression is true, the first
value is returned. If the logical expression is false, the second IIF
return value is returned.
NOTE: To count or sum ALL possible values of a field, do not use this
report variable technique. Instead, sort the database on the field,
and then create a data grouping in the report for the field.
To Count Fields
- Create a report variable containing an IIF function.
- In the logical expression of the IIF function, type the condition
you want to count. In the first IIF return value, type "1" (without
the quotation marks). In the second IIF return value, type "0"
(without the quotation marks).
- In the Variable Definition dialog box, under Calculate, select Sum.
To Sum Fields
- Create a report variable containing an IIF function.
- In the logical expression of the IIF function, type the condition
you want to test for when summing. In the first IIF return value,
type the name of the value (which must be numeric) you want to sum.
In the second IIF return value, type "0" (without the quotation
marks).
- In the Variable Definition dialog box, under Calculate, select Sum.
Example Using FoxPro for Windows
This example creates a database of different types of computers and
their prices. The report variable, DESKTOPCT, created in steps 5
through 8, counts the number of records whose type equals 'DESKTOP.'
The report variable, DESKTOPSUM, created in steps 9 through 11, sums
the price of all records whose type equals 'DESKTOP.'
- Use the following code to create the database:
CREATE TABLE test ;
(type C(20), price N(5))
INSERT INTO test (type, price) ;
VALUES ('DESKTOP', 1500)
INSERT INTO test (type, price) ;
VALUES ('LAPTOP', 2000)
INSERT INTO test (type, price) ;
VALUES ('DESKTOP', 4000)
- Use the Report Builder to create a new report.
- From the Report menu, choose Quick Report, then the OK button.
- From the Report menu, choose Title/Summary. Under Report
Summary, select Summary Band. Choose OK.
- From the Report menu, choose Variables, then choose the Add
button.
- In the Variable Name text book, type "desktopct" (without the
quotation marks).
- Choose the Value To Store button, which opens the Expression Builder
dialog box. Under Value To Store In Variable Desktopct, type the
following:
IIF(test.type = 'DESKTOP',1,0)
Choose OK.
- In the Variable Definition dialog box, under Calculate, select
Sum. Choose OK. (This variable counts the number of records whose
type equals 'DESKTOP').
- In the Report Variables dialog box, choose the Add button. In the
Variable Name text book, type "desktopsum" (without the quotation
marks).
- Choose the Value To Store button, which opens the Expression
Builder dialog box. Under Value To Store In Variable Desktopsum,
type the following:
IIF(test.type = 'DESKTOP',test.price,0)
Choose OK.
- In the Variable Definition dialog box, under Calculate, select
Sum. Choose OK twice to return to the Report Layout window. (This
variable sums the PRICE field for all records whose type equals
'DESKTOP').
- From the toolbox, select the Field tool.
- Place a field object in the Summary band. In the Report Expression
dialog box, choose the Expression button. Under Variables, double-
click DESKTOPCT. Choose OK to return to the Report Layout window.
- From the toolbox, select the Field tool again.
- Place a field object in the Summary band. In the Report Expression
dialog box, choose the Expression button. Under Variables, double-
click DESKTOPSUM. Choose OK to return to the Report Layout window.
- From the Report menu, choose Page Preview. Note that the variable
DESKTOPCT displays "2," and DESKTOPSUM displays "5500."
|