Microsoft KB Archive/106659

= How to Sum or Count Specific Fields in a Report =

Article ID: 106659

Article Last Modified on 12/3/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft FoxPro 2.5b
 * Microsoft FoxPro 2.5a
 * Microsoft FoxPro 2.5b
 * Microsoft FoxPro 2.0
 * Microsoft FoxPro 2.5b for MS-DOS
 * Microsoft FoxPro 2.5a
 * Microsoft FoxPro 2.5b for MS-DOS

-



This article was previously published under Q106659



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

 * 1) Create a report variable containing an IIF function.
 * 2) In the logical expression of the IIF function, type the condition you want to count. In the first IIF return value, type 1 . In the second IIF return value, type 0.
 * 3) In the Variable Definition dialog box, under Calculate, select Sum.

To Sum Fields

 * 1) Create a report variable containing an IIF function.
 * 2) 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.
 * 3) 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 . 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.</li> In the Variable Definition dialog box, under Calculate, select Sum. Choose OK. (This variable counts the number of records whose type equals 'DESKTOP').</li> In the Report Variables dialog box, choose the Add button. In the Variable Name text book, type desktopsum .</li> 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.</li> 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').</li> From the toolbox, select the Field tool.</li> 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.</li> From the toolbox, select the Field tool again.</li> 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.</li> From the Report menu, choose Page Preview. Note that the variable DESKTOPCT displays "2," and DESKTOPSUM displays "5500."</li></ol>

Additional query words: VFoxWin FoxDos FoxWin akz

Keywords: KB106659

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.