473,503 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dsum Issue

28 New Member
Hello,

Thanks to a previous helper I found some great Dsum info here:

http://www.mvps.org/access/general/gen0018.htm

Unfortunately I can't get the code to work for my requirements.

I'm trying to sum the payment received ($) field within my 'tbl_treatment payments' table for every record that has the field "payment method" = cash.

The code I'm using is:

=DSum("payment received","tbl_treatment payments","payment method = cash")

I'm sure my mistake is something simple, however I can't for the life of me figure it out. It's driving me crazy.

This is all for a report in Access 2000.

Thanks!
Oct 15 '07 #1
19 3362
MikeTheBike
639 Recognized Expert Contributor
Hello,

Thanks to a previous helper I found some great Dsum info here:

http://www.mvps.org/access/general/gen0018.htm

Unfortunately I can't get the code to work for my requirements.

I'm trying to sum the payment received ($) field within my 'tbl_treatment payments' table for every record that has the field "payment method" = cash.

The code I'm using is:

I'm sure my mistake is something simple, however I can't for the life of me figure it out. It's driving me crazy.

This is all for a report in Access 2000.

Thanks!
Hi

Looking at the site you references above, I suggest you look at the second line given

For numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")

For strings: (note the apostrophe before and after the value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")

For dates:
DLookup("FieldName" , "TableName" , "Criteria= #date#")

on the basis that 'cash' is a text field ??


MTB
Oct 15 '07 #2
dozingquinn
28 New Member
Looking at the second example, I tried the apostrophe before and after the criteria - but alas it still didn't work.

=DSum("payment received","tbl_treatment payments","payment method= ‘cash’")

Payment method is a text field.
Oct 15 '07 #3
Lysander
344 Recognized Expert Contributor
Looking at the second example, I tried the apostrophe before and after the criteria - but alas it still didn't work.

=DSum("payment received","tbl_treatment payments","payment method= ‘cash’")

Payment method is a text field.
You have spaces in your field names, so you need to enclose them in brackets

=dsum("[payment received]","tbl_treatment payments","[Payment method]='cash'")

You might have to put the table name in [ ] as well .
Oct 15 '07 #4
dozingquinn
28 New Member
Thanks for the suggestion,

I tried the brackets - (both suggestions) - but I still return a #error value.

:(
Oct 15 '07 #5
puppydogbuddy
1,923 Recognized Expert Top Contributor
Thanks for the suggestion,

I tried the brackets - (both suggestions) - but I still return a #error value.

:(
Try it using the nz (null to zero) function like this:
=dsum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'")
Oct 16 '07 #6
dozingquinn
28 New Member
Thankyou Puppy!

I now get a value! (Total cash sales across the whole record set). I see now (after reading up on NZ functions) that the 0's must have been causing issues.

As my report is grouped by month, is it possible to extend the formula so that it only dsum's the values within the given month?

Looking round I tried to decipher it myself - however my stab in the dark hasn't been fruitful:

=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND “[Payment date]” = & 'MM())
Oct 16 '07 #7
puppydogbuddy
1,923 Recognized Expert Top Contributor
Thankyou Puppy!

I now get a value! (Total cash sales across the whole record set). I see now (after reading up on NZ functions) that the 0's must have been causing issues.

As my report is grouped by month, is it possible to extend the formula so that it only dsum's the values within the given month?

Looking round I tried to decipher it myself - however my stab in the dark hasn't been fruitful:

=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND “[Payment date]” = & 'MM())
Try this syntax, (using February as an example).
=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND Month(“[Payment date]”) = 2)

the 0's must have been causing issues
It isn't the zeros causing the issues, it is the nulls(where nothing is entered for payment). The nz function converts the nulls to zeros to resolve the issues.
Example:
null + $50 = null
0 +$50 = $50
Oct 16 '07 #8
dozingquinn
28 New Member
Thanks again.

Nearly there...

When I indicated that my report was grouped by months I should have also clarified that the report autogenerates all the months. Is it possible to tie the month criteria to the month in question - rather than hardcoding it to a specific month?

Or am I going about this the wrong way?
Oct 16 '07 #9
puppydogbuddy
1,923 Recognized Expert Top Contributor
Thanks again.

Nearly there...

When I indicated that my report was grouped by months I should have also clarified that the report autogenerates all the months. Is it possible to tie the month criteria to the month in question - rather than hardcoding it to a specific month?

Or am I going about this the wrong way?
If you have a FiscalMonth in your table, you can do something like this:
Try this syntax, (using February as an example).
=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND Month(“[Payment date]”) = [FiscalMonth])
Oct 16 '07 #10
FishVal
2,653 Recognized Expert Specialist
If you have a FiscalMonth in your table, you can do something like this:
Try this syntax, (using February as an example).
=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND Month(“[Payment date]”) = [FiscalMonth])
:-)

Nz function does happily nothing here.
nz("[payment received]",0) = "[payment received]" always

Anyway aggregate functions deal with Nulls pretty nice. Moreover replacing Nulls with 0's will have no result with DSum and will cause erroneous result with DAvg and DStDev.

Regards,
Fish
Oct 16 '07 #11
puppydogbuddy
1,923 Recognized Expert Top Contributor
:-)

Nz function does happily nothing here.
nz("[payment received]",0) = "[payment received]" always

Anyway aggregate functions deal with Nulls pretty nice. Moreover replacing Nulls with 0's will have no result with DSum and will cause erroneous result with DAvg and DStDev.

Regards,
Fish
Fish,
Firstly......if the NZ function had no effect, go back and read posts #'s 4, 5, 6, and 7 and then explain to me how come the payments started appearing in the result set for the first time when the only change I made was to add the NZ function......

Secondly, the OP is not using DAvg or DStDev, so that is not relevant here..
Oct 17 '07 #12
FishVal
2,653 Recognized Expert Specialist
Fish,
Firstly......if the NZ function had no effect, go back and read posts #'s 4, 5, 6, and 7 and then explain to me how come the payments started appearing in the result set for the first time when the only change I made was to add the NZ function......
Apologizes, PDB, but do you want me to explain what might happening in OP's project?

Take a look at the expression.

=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND Month(“[Payment date]”) = [FiscalMonth])

The first argument passed to DSum function is what is returned by Nz function and nz("[payment received]",0) will always return string "[payment received]". To make Nz to run on each record it is better to include it to string given to DSum as Expression argument.

=DSum("nz([payment received],0)", ...

But that is never needed as one of the purposes of Null is to let aggregate functions to treat a value as absent.

Secondly, the OP is not using DAvg or DStDev, so that is not relevant here..
I wanted to state that Null values are actually friends of aggregate functions and never confuse them.

P.S. To make sure I'm not making a silly mistake I've tried
=DSum("fld1", "tbl1")
=DSum(Nz("fld1",0), "tbl1")
=DSum("Nz([fld1],0)", "tbl1")
in unbound form control
All them return the same. fld1 values are Null, 100, Null, 200
Oct 17 '07 #13
FishVal
2,653 Recognized Expert Specialist
To dozingquinn.

I suggest you before putting an expression to form/report control try to run it in VBA immediate window. This will give you a more comprehensive error description.
Oct 17 '07 #14
puppydogbuddy
1,923 Recognized Expert Top Contributor
Apologizes, PDB, but do you want me to explain what might happening in OP's project?

Take a look at the expression.

=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'" AND Month(“[Payment date]”) = [FiscalMonth])

The first argument passed to DSum function is what is returned by Nz function and nz("[payment received]",0) will always return string "[payment received]". To make Nz to run on each record it is better to include it to string given to DSum as Expression argument.

=DSum("nz([payment received],0)", ...

But that is never needed as one of the purposes of Null is to let aggregate functions to treat a value as absent.



I wanted to state that Null values are actually friends of aggregate functions and never confuse them.

P.S. To make sure I'm not making a silly mistake I've tried
=DSum("fld1", "tbl1")
=DSum(Nz("fld1",0), "tbl1")
=DSum("Nz([fld1],0)", "tbl1")
in unbound form control
All them return the same. fld1 values are Null, 100, Null, 200

Fish,
I understand what you are saying, what I don't understand is this:

Expression before NZ function:
=dsum("[payment received]","tbl_treatment payments","[Payment method]='cash'")

Comment from OP:
Thanks for the suggestion,
I tried the brackets - (both suggestions) - but I still return a #error value.

Expression with NZ function:
=dsum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'")

Comment from OP:
Thankyou Puppy!
I now get a value! (Total cash sales across the whole record set).
Oct 17 '07 #15
FishVal
2,653 Recognized Expert Specialist
That is a classic illusion. You are trapped with what you see. Sure nobody but the OP can explain this.
Oct 17 '07 #16
dozingquinn
28 New Member
Hello again,

A big thanks to Puppy & Fish for trying to help me out - all much appreciated. I don't want to buy into any argument, however my original dsum statement wasn't working without adding the nz value.

I have since tried the suggestion:

=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'") And Month([Payment date])="[real month]"

_where [real month] is essentially another field with the expression month([payment date]).

Unfortunately this didn't work. Infact it gave a zero sum. Taking out the AND Month... returns a value which dsums the whole record set again.

Is there an easier method in just trying to Dsum the criteria for all months - without having to hardcode the month value?
Oct 18 '07 #17
FishVal
2,653 Recognized Expert Specialist
Hello again,

........
=DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash'") And Month([Payment date])="[real month]"

......
Hi, dozingquinn.

Actually you are going with a very strange syntax.
At least it should look like
Expand|Select|Wrap|Line Numbers
  1. =DSum(nz("[payment received]",0),"tbl_treatment payments","[Payment method]= 'cash' And Month([Payment date])=" & [real month])
  2.  
To suggest you an exact syntax I need answers for the following questions:
  • Explain please what you are referring to with [real month]?
  • Table field, bound control, unbound control?
  • What [real month] supposed to contain?
  • Month number, month name or abbreviation?
Oct 18 '07 #18
dozingquinn
28 New Member
Hi FishVal - I forgot to note in my previous code that I'm now working off a query - rather than a table source:

=DSum(nz("[payment received]",0),"qry_treatment payments","[Payment method]= 'cash' And Month([Payment date])=" & [real month])

Explain please what you are referring to with [real month]?
[Real month] was a new field created in the query that extracts the numbered month of the [payment date] field. By setting it up I was attempting to get records that returned a [payment method] = cash - and that had a [payment date] equal to each grouped month.

[*] Table field, bound control, unbound control?
I originally set the report up to source from [tbl_treatment payments], however as I had to create a [real month] field - I changed the record source to [qry_treatment payments].

All fields are bound to the query source

[*] What [real month] supposed to contain?
As explained above - the number of the month in [payment date]. e.g a [payment date] of 01/Feb/07 would return a [real date] of 2.

Thanks.
Oct 19 '07 #19
FishVal
2,653 Recognized Expert Specialist
.......

=DSum(nz("[payment received]",0),"qry_treatment payments","[Payment method]= 'cash' And Month([Payment date])=" & [real month])

[Real month] was a new field created in the query that extracts the numbered month of the [payment date] field. By setting it up I was attempting to get records that returned a [payment method] = cash - and that had a [payment date] equal to each grouped month.
.......................
As explained above - the number of the month in [payment date]. e.g a [payment date] of 01/Feb/07 would return a [real date] of 2.

Thanks.
????

You retrieve Month([Payment date]) as [real month] in query [qry_treatment payments], then you compare Month([Payment date]) and [real month] in filter argument of DSum function.

So what are you expecting from that comparison?
Oct 19 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3336
by: Rolan | last post by:
I'm using Access 97 and need some assistance in sorting out a proper DSum expression, or maybe even DCount might be an alternative. I have tried numerous combinations, but with no apparent success....
1
3946
by: Dalan | last post by:
This seems a bit odd, but I have not been able to persuade Access 97 to format several DSum total columns in Currency - Standard format. It appears to be defaulting to a general number format...
2
11560
by: Dalan | last post by:
I seemed to be having problems with structuring the use of NZ with a DSum expression. Having tried numerous variations of the expression without success, I'm asking for assistance. First some...
0
1584
by: Dalan | last post by:
I have a select query with a few DSum expressions, but when I add a between and ending date parameter to Date Sold, the results show #Error in two (discount and net sales) of the four gross total...
1
1905
by: Marc Aube | last post by:
Is there a web site that can ofer some help. The quotation marks are posing an issue as well as other items for this function. I have some books but they are not consistent in their use of the code...
1
4417
by: phaddock4 | last post by:
Being fairly inexperienced at Access 2000, i've been reading many posts here for the last several days, and testing myself to find the best approach to do the following in A2K: SET UP: I have...
3
2977
by: technocraze | last post by:
Hi community experts, I am having an isue with Dsum function that is used to count the total number for a particular field (intake) at the textbox afterupdate event with condition/ criteria...
3
4684
patjones
by: patjones | last post by:
Good morning all: In what seems like an ongoing saga to make the DSum function do what I need it to, I am now having trouble with a user-defined function in my VBA module. Here's the offending...
9
5408
by: BlackJack17 | last post by:
Alright you all have been so helpful with everything else that I thought I'd run one more issue by you. I have put together a report that is driven by a query. This query (Unique Records) returns...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.