Running Sum Using Query (1 Viewer)

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
Hello,

This query created to produce Running Sum of the invoices of same customer. But even it producing double amount when there only one invoice for that particular customer. Previous I used this query and it was working. May be there has been changed somewhere which I didnt clear about.

Example:

Barratt Associates, this client has invoice of 130 only but ruining sum is showing 260
It should make all invoices amounts total when there are more than one invoices.

Where is wrong in my query? Can someone help me there.... I have attached the db.

Thanks in advance
Ashfaque
 

Attachments

  • RunningSumQuery.accdb
    452 KB · Views: 74

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:41
Joined
Aug 30, 2003
Messages
36,125
I'm not clear on the issue, the item you mentioned is only 130 when I run it:

1691770572796.png
 

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
But at me it is strange....

1691771610097.png
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:41
Joined
Aug 30, 2003
Messages
36,125
You obviously have more/different data in that db than is in the sample. Does the sample work properly for you?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:41
Joined
Aug 30, 2003
Messages
36,125
That's odd. I wonder if the untrusted status of the file on my computer is affecting the calculation. At a glance I don't see why it would.
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,299
Expr1 is your calc that you add to Amount.
Code:
Nz(DSum("Amount","T_CrSalesInvoice","CustomerCode = " & [CustomerCode] & " And (InvDate < #" & [InvDate] & "# Or (InvDate = #" & [InvDate] & "# And InvNum < " & [InvNum] & "))"),0)

Then you can see how it happens
1691772367633.png
 

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
Better I will attach here my FE and BE with min records and tbls etc so that it would be easy for you.

Please enter in db with Shift +Enter to see the tbls, queries, forms etc.

You may directly run form "F_CrSalesInvStatus_Master". This was created to clear OLDEST invoices as and when paid by customers..means adjustement of payment from invoice to invoice..

Take the example of Barratt Associates and add new payment in upper area...just enter date and amount...not need to feed other fields.

The strange thing is it showing overall total correct at the left text boxes. But not in 2 query grids....strange....

Regards,
 

Attachments

  • GREC_FE.zip
    711.7 KB · Views: 61

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,299
You need to work it out.
You will notice that for all the others the starting amount is 0 for the first record of the group.
I *thought* it might be because it is a single record, but you have others there that are single records and they have 0 for Expr1.

You need to determine why that is.

Broke it down even more.
Code:
Expr2: DSum("Amount","T_CrSalesInvoice","CustomerCode = " & [CustomerCode] & " And InvDate < #" & [InvDate] & "#")
Expr3: DSum("Amount","T_CrSalesInvoice","CustomerCode = " & [CustomerCode] & " AND  InvDate = #" & [InvDate] & "#  And InvNum < " & [InvNum])

Up to you now.

1691773604402.png
 

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
Did some one run this form ?

1691778582436.png


When we receive payment 130, it should clear first circled invoice and then of 290 and 50.

The query is doubling amount at first record which I want to stop and accumulate from next same customer only then query result would be ok

Any other thoughts ?

You obviously have more/different data in that db than is in the sample. Does the sample work properly for you?
previously all the records were showing properly with same query but since I added Barratt Associates, it started producing the above errors....
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,299
I got rid of fields not needed for this problem.
I am wondering if it is a date issue, as you have not formatted the dates, yet are using # ?

1691779474054.png


Yes, it is a date issue.

I changed Expr2 to be
Code:
DSum("Amount","T_CrSalesInvoice","CustomerCode = " & [CustomerCode] & " And InvDate < #" & Format( [InvDate],"mm/dd/yyyy") & "#")

and now get

1691779884837.png


So format your dates correctly.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,299
Last column is RunningAmount2
Code:
RunningAmount2: [Amount]+Nz(DSum("Amount","T_CrSalesInvoice","CustomerCode = " & [CustomerCode] & " And (InvDate < #" & Format([InvDate] ,"mm/dd/yyyy") & "# Or (InvDate = #" & Format([InvDate] ,"mm/dd/yyyy") & "# And InvNum < " & [InvNum] & "))"),0)

PBaldy is in the USA I believe, or at least uses the default Access date format.
That would explain why it works for him.

1691780394954.png
 

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
Thanks Gasman,

that was the exact point I missed. My date format was different than the normally I used.

Thanks for pointing ....very much appreciated.....
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:41
Joined
Aug 30, 2003
Messages
36,125
PBaldy is in the USA I believe, or at least uses the default Access date format.
That would explain why it works for him.

Correct on both counts, but I'm aware of that issue and should have noticed it. 😟
 

Ashfaque

Student
Local time
Today, 13:11
Joined
Sep 6, 2004
Messages
894
How about if we need both British and USA date format to work....I mean "dd/mm/yyyy" or "mm/dd/yyyy"

Could it be ?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,299
Dates are just numbers, so keep it as it is or use format yyyy-mm-dd.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:41
Joined
Aug 30, 2003
Messages
36,125
How about if we need both British and USA date format to work....I mean "dd/mm/yyyy" or "mm/dd/yyyy"

Could it be ?
You can format any way you want for users, it's vba that requires US format:

 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:41
Joined
Feb 19, 2002
Messages
43,275
How about if we need both British and USA date format to work....I mean "dd/mm/yyyy" or "mm/dd/yyyy"
NEVER format dates that you will later use to sort on or compare. WHY? because formatting them turns them into a string. Strings sort like strings. Character by character, left to right. So, 01/02/2023 is less than 02/01/2020 because 01 is less than 02. Internally, dates are stored as double precision numbers with the integer portion representing the number of days since 12/30/1899 (for Jet/ACE) and the decimal representing a point in time. That means 12/30/1899 6 PM would = 0.75 12/31/1899 6 AM 2 would be 1.25. 12/29/1899 12 PM would be -1.5.

If you have to format the date because you are creating the SQL in a string, you need to use an unambiguous format such as yyyy/mm/dd OR use the US standard of mm/dd/yyyy since that is what SQL Server assumes if you give it an ambiguous string. Is 01/02/2023 Jan 2 or Feb 1? SQL Server will assume Jan 2.
 

Users who are viewing this thread

Top Bottom