Understanding the use of Domain Aggregate functions in a query (2 Viewers)

wmphoto

Registered User.
Local time
Today, 01:46
Joined
May 25, 2011
Messages
77
Hi. I'm currently trying to create a query which works out from the following information what the outstanding balance for each invoice in a table.

  • The total amount that was invoiced
  • The client the invoice is for
  • The total that client has ever been invoiced
  • The total that client has ever paid
  • The due date on the invoice
The idea being, if in total the client 'ABC' has been invoiced £5000 and paid £4000 then they have a balance of £1000 to pay. If their most recent invoice is for £400 then this invoice still has the entire £400 outstanding... if the invoice before that was for £800 then that invoice has a balance outstanding of £600 (£200 having been paid)... and all earlier invoices have been paid in full.

I can then easily bring up a list of unpaid invoices for each client which can go on the bottom of any new invoices as a reminder. I'm looking to do this rather than simply use the total balance outstanding (£1000 in the above example) because:
a) different amounts would have different due dates, some of the outstanding balance will be overdue, some won't.
b) I'd rather be able to outline to each client exactly what they haven't paid than give them a total. I just feel they're more likely to respond positively if the reminder says exactly which invoices are overdue and when they should have been paid by.


I had this idea on reading about the domain aggregate functions that they could be used to achieve this, but now I'm not sure I'm understanding them correctly. It could be I'm barking up the wrong tree.


I'll refer to this example, as it's much simpler than my own example
http://www.techonthenet.com/access/functions/domain/dsum.php


In it, they use the example function:
Code:
DSum("UnitPrice", "Order Details", "OrderID = 10248")
to sum the [UnitPrice] field from the [Order Details] table for all records where the [OrderID] field is 10248


Simple enough, except this would return the exact same figure for all rows in the query in which it is used, which is why I think I'm barking up the wrong tree.


Say you were using this function in a query where different rows referred to different OrderIDs and you wanted the function in each row to sum the unit prices in Order Details where the Order ID matched that specific row.


Instead of "OrderID = 10248", there might be a column in your query called [OrderIDtosum] and you'd think you could have the expression read: "OrderID = [OrderIDtosum]" or something similar. Except it appears you can't. You have to simply use a value such as 10248 that is fixed and doesn't vary from row to row.


If this is the case then I don't think domain aggregate is the function that will work for me, but I wanted to check.
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:46
Joined
Aug 30, 2003
Messages
36,126
In your example it would be

"OrderID = " & OrderIDtoSum

But using domain aggregates in a query can be performance killers.
 

khurram7x

Registered User.
Local time
Today, 05:46
Joined
Mar 4, 2015
Messages
226
Something like:

Dim int1 As Integer
Dim text1 As String
int1 = 2
text1 = DSum("CurrentStockLevel", "tblEquipment", "invoice = " & int1 & "")
 

wmphoto

Registered User.
Local time
Today, 01:46
Joined
May 25, 2011
Messages
77
In your example it would be

"OrderID = " & OrderIDtoSum

But using domain aggregates in a query can be performance killers.

Thanks I'll give it a try... it all makes sense when you put it that way, I remember the help saying that the expression is a string, but wasn't really treating it as such.

Hopefully the performance hit shouldn't be too much of an issue, but I'll let you know. This query will only be run once whenever an invoice is produced, and it's just a small business so that'll be every now and again.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:46
Joined
Aug 30, 2003
Messages
36,126
No problem, post back if you get stuck.
 

wmphoto

Registered User.
Local time
Today, 01:46
Joined
May 25, 2011
Messages
77
Thanks, because I'm getting an error at the moment. My expression is:
Code:
DSum("Amount","Invoice Totals","Client = " & [ClientBal])
(access automatically puts the square brackets around ClientBal)

As for each invoice I want to Sum the 'amount' field in the 'invoice totals' query for invoices with the same client. ('Client' is the field in invoice totals that says which client the invoice for, and 'ClientBal' is the field in this query that says which client the invoice is for).

This is step 1, step too will be to expand the expression so that it sums only those invoices which are
a) to the same client
b) have an earlier due date

The result will be a field which tells me for each invoice the total amount that client was invoiced on earlier invoices... using this alongside the total that client has paid in total, I can work out how much of this invoice's balance has been paid.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:46
Joined
Aug 30, 2003
Messages
36,126
Can you post the db here?
 

Users who are viewing this thread

Top Bottom