Microsoft KB Archive/120187

= PRB: Query to a Report Causes an Inaccurate Sum or Count =

Article ID: 120187

Article Last Modified on 12/3/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft FoxPro 2.0
 * Microsoft FoxPro 2.6 for MS-DOS
 * Microsoft FoxPro 2.6a Standard Edition
 * Microsoft FoxPro 2.6 Standard Edition
 * Microsoft FoxPro 2.6a Standard Edition
 * Microsoft FoxPro 2.6 for SCO/UNIX

-



This article was previously published under Q120187



SYMPTOMS
When you are running a query to a report using two or more databases with a one-to-many relationship and the report is counting or performing other math functions on fields from the parent database, the count or value from the math functions is inaccurate.



RESOLUTION
To get an accurate count, you need to create a user-defined function (UDF). For an example of a UDF that produces the correct count, see "Workaround" in the "More Information" section below.



MORE INFORMATION
When the report performs a count or any math function, it looks to the source database for its information. If the data comes from a query, the source is a temporary table called QUERY. If the query had two or more tables in it with a one-to-many relationship, there will be a matching parent field for each child field.

Steps to Reproduce Problem

 * 1) Use the CUSTOMER.DBF and INVOICES.DBF tables that are found in the \TUTORIAL subdirectory.
 * 2) Create a report that contains the CUSTOMER.CNO, CUSTOMER.STATE, INVOICES.INO, and INVOICES.ITOTAL fields. Just put the field names in the report and mark the fields coming from CUSTOMER so they don't print repeated values.
 * 3) Group the report by state.
 * 4) In the group footer band, place the CNO field and select Calculate and then Count.
 * 5) Save the report.
 * 6) Open a query and add the CUSTOMER and INVOICES tables.
 * 7) Add the fields listed above to the fields list in the query.
 * 8) Order the query by state.
 * 9) Output the query to the report saved in step 5 above.

If you look at the group from the state of "GA", you will see that the count is 7 but that there are only four different companies.



WORKAROUND
To obtain the correct count, you can use the following UDF. FUNCTION mycount PARAMETERS x  recnum = RECNO mygroup = state SELECT DISTINCT customer.cno, customer.state; FROM customer, invoices; WHERE invoices.cno = customer.cno; AND customer.state = mygroup; INTO CURSOR counter COUNT TO x  SELECT QUERY GO recnum RETURN x

