Filter by one "Many" value in a query

Ok, this is been a serious PITA for me - I've got an Access 2000 database for a car dealership, and I'm having trouble taking values that I calculate in a form and instead calculate them in a query.

For example, we have a payment table to track customers' monthly payments.  It is setup with a primary key for each payment, and the invoice number from the buys table is associated with it.  

paymentID     InvoiceNumber   PaymentAmount   PaymentDate  Receipt Number
1                          123                   400.00                 9/21/04          239487
2                          143                   142.78                 8/30/04          059201
3                          123                   400.00                 10/04/04        239498
4                          102                   350.00                 7/23/04          098520

I have a query take these values plus all the sales info included by the invoice number such as vehicle and customer info (date sold, monthly payment, total financed, etc), then a form that filters this by invoice number with a combo box.  

Based on what is loaded into the form from the query, the payments are summed up and past due, months past due, # of payments made, # of payments left, interest-based calculation for early payoff etc etc are done.

Ok!  so I want to do a sort of "For/Next" loop like "For each distinct Invoice Number, sum up Payment value and store that sum".  How do I set a filter for another query to do that?


Invoice Number       LastPaymentMade       PaymentSum   PastDueAmount
102                         7/23/04                     350.00            700.00
123                         10/04/04                   800.00            400.00
143                          8/30/04                    142.78            300.78
Who is Participating?
InformativeConnect With a Mentor Commented:
So like if you have this

Invoice Number       LastPaymentMade       PaymentSum   PastDueAmount
102                         7/23/04                     250.00            700.00
102                         7/15/04                     125.00            700.00
123                         10/04/04                   800.00            400.00
143                          8/30/04                    142.78            300.78

your query will look something like this
SELECT Table1.invnumber, max(, sum(Table1.amount)
FROM Table1
group by Table1.invnumber;

with results something like this
number      Expr1001      Expr1002
102      7/1/04      375
123      8/5/04      800
143           8/30/04    142.78
shakparlAuthor Commented:
The payments themselves are displayed in a subform with the invoice number as the parent field, and the sum of the payments is calculated on the fly in the form.
In a query the GROUP BY clause will cause it to sum the values in the other column by default
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

shakparlAuthor Commented:
Ok, that sums the payments up beautifully, but it's difficult to work with that query.  I can include a number of other values from other query like customer name and date sold, but when I try to add an expression that is calculated from another query, it complains about "Invalid Use of Null", or something about not being included in an aggregate function.

Should I just redo the expressions in this new paymentsum query?

e.g. I need the total financed, monthly payment and such to get all this together, but those are like this:

Monthly Payment: CCur((CLng(100*((([Total Financed]+[IntAccrDFP1])*[Periodic Rate])/(1-((1+[Periodic Rate])^((-1)*Buys.[Number of Payments]))))))/100)

 in the original "Buyers Order Query".  Some of them could be 0/Null, is that what the problem is?
shakparlAuthor Commented:
I'm trying to rebuild the expression from the Buyers Order Query and keep getting this:

3122 You tried to execute a query that does not include the specified expression <name> as part of an aggregate function.

Including expressions from other queries never caused me trouble before.  What is with this error when using GROUP BY?
You said:  (when I try to add an expression that is calculated from another query ... something about not being included in an aggregate function.)

This is equivalent to Access Design view when you add the "group by" row all columns must have either "SUM" or "GROUP BY" or other "aggregate instruction" which determines if they will create a new row on each uniqie value for that column or add all of the values together for that cell based on other columns unique groupings.

Take this oversimplified query for an example:

SELECT, Sum(Table1.cost) AS SumOcost, FROM Table1 GROUP BY,;

Notice how three columns are pulled from table1 yet only two are grouped by?  We could not simply include a column without applying some form of ranged calculation on the remaining column because several rows of those values will need to be USED in some way in this case we SUM them for the total for that grouping.

We could Average them or take a Count of them just as well but if you had five rows in a group it needs an aggregate handler to deliver a single value for those five entries under that group/subgroup.

I hope that clears it up some.
Notice in the above example query we combined invoice 102 into a single row it had to know what to do with the other columns.  The Max function would have returned 250.  Average function would return 187.5, sum gave us 375, Count would return 2.. etc.  the function which takes the many entries and returns the single entry is the aggregate.  

In the example I used Max to return just the most recent date.
shakparlAuthor Commented:
Thanks for the response.  When I properly set all values in the query to Group By, I get the "invalid use of null error", which (from what I've read) is just Access choking on a null value, and not related to this thread.  Yuk, I have to rebuild that query with Nz([field],0) since a lot of them are null.

Anyway, thanks again for the useful info!  You've been really helpful!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.