Should i use DSUM?

paulcraigdainty

Registered User.
Local time
Today, 05:12
Joined
Sep 25, 2004
Messages
74
I want to add to values based on if yes or no is selected in an option group.

My thinking is to use DSUM. When 'yes' is selected I want to grab a value from one field and add it to a value in another field giving a running total. I am currently using the following code but it throws back an error. Is DSUM the way to go?

Case 1

txtTotal.Value = DSum("[s1]", "[s2]")
 
That isn't how DSUM works. DSUM is to sum a single field using

DSum("[YourFieldName]", "YourTableName", "[PutCriteriaFieldHere]=" & Me.WhateverCriteriaControlHere)

Or to sum everything in the field with no criteria:

DSum("[YourFieldName]", "YourTableName")

To do more than one field you would use:

Me.YourTextBox = DSum("[YourFieldName1]", "YourTableName") + DSum("[YourFieldName2]", "YourTableName")
 
I can't get my head around this, i want to add up a set of values from different text boxes. Currently i'm using the following in the default value property of the text box which I want to display the total:

=txt1 + txt2


txt1 contains value 50
txt2 contains value 40

The result displays 5040 - I need it to display 90
 
Well, your problem is data typing. In a text box, the values are .... wait for it .... TEXT formats. For which the "+" operator is CONCATENATION. And of course, 50 concatenated with 40 is 5040.

If you want to generate the sum of txt1 and txt2 AS NUMBERS, convert them to numbers first, THEN add them. And if you need the answer also as a text string, convert the sum back.

txtsum = CStr( CLng(txt1) + CLng(txt2) )

Your DSum is called a DOMAIN AGGREGATE. It is the summation aggregate of ONE FIELD across the entire domain. The range of the domain is all records in the table (or query) in which the selected field exists. The domain itself is defined as the records left over after the filtering clause (3rd argument) is successfully applied. In the trivial case (no 3rd argument), the range and domain are the same.
 

Users who are viewing this thread

Back
Top Bottom