Writing Multiple criteria in DSUM function (1 Viewer)

Local time
Today, 16:43
Joined
Nov 23, 2018
Messages
31
I want to calculate previous month total quantity where generic and category fields are the same the quantity should sum up in access query
Code:
Last Month Qty: DSum("[Quantity]","[Inventory Transactions Extended]","[Transaction Types].[Add/Remove]='Removal' and [Inventory.Generic] = " & [Generic] and [Inventory].[Category] =’" &[Category]& ”’” and [Inventory Transactions Extended].[Created Date] >#DateSerial(Year(Date()),IIf(Month(Date())=1,12,Month(Date())-1),1)# and [Inventory Transactions Extended].[Created Date] < #DateSerial(Year(Date()),Month(Date()),0)#)
In this Generic field type is number while Category field type is short text. I am trying to use this one but this is not working.
Kindly help me how to figure it out.

Thanks
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:43
Joined
Sep 21, 2011
Messages
14,269
If you use a domain function, it has to be on a single domain?
You appear to be trying to look up values from more than one. :(
Make a query to join all tables/queries that you need and use that.
Also put your criteria into a string variable.
Debug.print that until you get it correct, the use that in the function.
 

plog

Banishment Pending
Local time
Today, 07:43
Joined
May 11, 2011
Messages
11,645
"[Transaction Types].[Add/Remove]='Removal' and [Inventory.Generic] = " & [Generic] and [Inventory].[Category] =’" &[Category]& ”’” and [Inventory Transactions Extended].[Created Date] >#DateSerial(Year(Date()),IIf(Month(Date())=1,12,Month(Date())-1),1)# and [Inventory Transactions Extended].[Created Date] < #DateSerial(Year(Date()),Month(Date()),0)#)

1. Don't try and eat an elephant in one bite. Make it work with one criterion, then add a second, then a third, etc. etc. Start with something that actually works. Then slowly add to it so you know which part is the part that's the trouble.

2. DSum only knows about the datasource you tell it about in the 2nd argument. The only data the criteria has has access to is the data you pass it (e.g. Date() or a field from the query itself). It can't reference by datasource.field notation a filed not in the datasource you told it to add up.

3. & is for concatenating strings, AND is for concatenating logical comparisons within the string.

"Plog Is" & "Awesome"

"(X=True) AND (Y=False)"

I see you tried to use AND outside quote marks to string 2 variables together. Doesn't work like that.

4. I don't know if your date comparisons work in all cases. Not spending a lot of mental energy stepping through it, but I would check the edge cases---does it work in January? Does it work on the first and last day of a month? Again, might be right, but making sure dates find the correct prior month is tricky.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:43
Joined
Sep 21, 2011
Messages
14,269
Also if you use DateSerial() you do not need the # character
 
Local time
Today, 16:43
Joined
Nov 23, 2018
Messages
31
1. Don't try and eat an elephant in one bite. Make it work with one criterion, then add a second, then a third, etc. etc. Start with something that actually works. Then slowly add to it so you know which part is the part that's the trouble.

2. DSum only knows about the datasource you tell it about in the 2nd argument. The only data the criteria has has access to is the data you pass it (e.g. Date() or a field from the query itself). It can't reference by datasource.field notation a filed not in the datasource you told it to add up.

3. & is for concatenating strings, AND is for concatenating logical comparisons within the string.

"Plog Is" & "Awesome"

"(X=True) AND (Y=False)"

I see you tried to use AND outside quote marks to string 2 variables together. Doesn't work like that.

4. I don't know if your date comparisons work in all cases. Not spending a lot of mental energy stepping through it, but I would check the edge cases---does it work in January? Does it work on the first and last day of a month? Again, might be right, but making sure dates find the correct prior month is tricky.
Thanks Alot Plog, I try to apply criteria one by one.
 
Local time
Today, 16:43
Joined
Nov 23, 2018
Messages
31
I want to take last date of month that was two months before (As this month is July I want to take last date of May), I am using this formula
Code:
=DSum("[Quantity]","[Inventory Transactions Extended]","[Inventory Transactions].[Created Date]> DateAdd("d",-1,DateSerial(Year(DateAdd("m",1,Date())),Month(DateAdd("m",-1,Date())),1)) and [Inventory Transactions].[Created Date]< DateSerial(Year(Date()),Month(Date()),0) and [Transaction Types].[Add/Remove]='Removal' and [Inventory.ID] = " & [ID])
But this is returning error. As I replace DateAdd function by mentioning dates in above formula it works well.
Code:
=DSum("[Quantity]","[Inventory Transactions Extended]","[Inventory Transactions].[Created Date]> #05-31-2022# and [Inventory Transactions].[Created Date]< DateSerial(Year(Date()),Month(Date()),0) and [Transaction Types].[Add/Remove]='Removal' and [Inventory.ID] = " & [ID])
I don' know how to fix. Please guide.
Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:43
Joined
May 7, 2009
Messages
19,237
you try this:
Code:
=DSum("[Quantity]","[Inventory Transactions Extended]","[Inventory Transactions].[Created Date]> DateSerial(Year(Date()),Month(Date())-1,0) and [Inventory Transactions].[Created Date]< DateSerial(Year(Date()),Month(Date()),0) and [Transaction Types].[Add/Remove]='Removal' and [Inventory.ID] = " & [ID])
 
Local time
Today, 16:43
Joined
Nov 23, 2018
Messages
31
you try this:
Code:
=DSum("[Quantity]","[Inventory Transactions Extended]","[Inventory Transactions].[Created Date]> DateSerial(Year(Date()),Month(Date())-1,0) and [Inventory Transactions].[Created Date]< DateSerial(Year(Date()),Month(Date()),0) and [Transaction Types].[Add/Remove]='Removal' and [Inventory.ID] = " & [ID])
Thanks Alot Arnelgp this worked well
 

daud

New member
Local time
Today, 05:43
Joined
May 28, 2009
Messages
14
People in the forum are very cooperative. Your assistance can ease people to a big extent please. Keep it on
 

Users who are viewing this thread

Top Bottom