Dsum (1 Viewer)

sbaud2003

Member
Local time
Tomorrow, 04:23
Joined
Apr 5, 2020
Messages
178
Dear Sirs,
I have a Table "total Cmp" in which the fields are finyear,section. Chamt and settlement_date
I want to get the value of total amount in the filed Chamt by cliking a command button and display the result in a textbox TCMP
I am using the code below but I am getting error type mismatch

Me.TCMP.value = FormatIndian(Nz(DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] And "[settelment_date] between [Text0] and [Combo4]")))


please help me
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:53
Joined
Sep 21, 2011
Messages
14,265
If the variable/control is text, surround it with single quote ', if date with #, if numeric, no character required.
Plus you are not concatenating the variables/controls correctly for text0 and Combo4 ?

Put all the criteria into a string and Debug.Print that string to see the result.
 

sbaud2003

Member
Local time
Tomorrow, 04:23
Joined
Apr 5, 2020
Messages
178
If the variable/control is text, surround it with single quote ', if date with #, if numeric, no character required.
Plus you are not concatenating the variables/controls correctly for text0 and Combo4 ?

Put all the criteria into a string and Debug.Print that string to see the result.
where to correct
 

namliam

The Mailman - AWF VIP
Local time
Tomorrow, 00:53
Joined
Aug 11, 2003
Messages
11,695
Make your code readable, it will help you debug it....

Me.TCMP.value = FormatIndian(Nz(DSum("Chamt"
, "total cmp"
, " finyear = " & [COMFY] & _
" and section = " & [TXTUC] And "[settelment_date] between [Text0] and [Combo4]")))

With each of the red coloured somethings wrong
The blue coloured also has a mistake.

Editted to add: storing computated values on average is a bad idea.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:53
Joined
Feb 19, 2013
Messages
16,607
DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] " And [settelment_date] between #" & [Text0] & "# and #" & [Combo4] & "#")

this assumes comfy and txtuc are numeric values

Note you do not need the nz function, dsum will return 0 if there are no values, you would use it for other domain functions such as dlookup.

tip for the future. If you get this sort of error, reduce the string to a single component (e.g. "finyear = " & [COMFY]) then keep adding until you get the error - then you will know where it is
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:53
Joined
Sep 21, 2011
Messages
14,265
Well I do not know what is numeric, date or text ? :(

However it should be along the lines of
Code:
Me.TCMP.value = FormatIndian(Nz(DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] And "[settelment_date] between #" [Text0] & "# and #" & [Combo4] &#")))

I use a constant format to get the format correct and easier to put together.

Code:
Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:53
Joined
Sep 21, 2011
Messages
14,265
You missed one ;)
Well I hoped the O/P would get the general idea. ?
If they put it into a string as I always do, then they would see the error immediately.?

It is called a learning process. :D
 

namliam

The Mailman - AWF VIP
Local time
Tomorrow, 00:53
Joined
Aug 11, 2003
Messages
11,695
It is called a learning process. :D
Which is why I highlighted where to look instead of putting in all kinds of fixes....

" Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. "
 

sbaud2003

Member
Local time
Tomorrow, 04:23
Joined
Apr 5, 2020
Messages
178
Thanks for the support . but still not resolve ... Code error is flashing with Red
I used in the Click event
(1)
me.TCMP.value=DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] " And [settelment_date] between "#" & [Text0] & "#" and "#" & [Combo4] & "#")

(2)
me.tcmp.value = DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] " And [settelment_date] between #" & [Text0] & "# and #" & [Combo4] & "#")
Error What to do?
 
Last edited:

Minty

AWF VIP
Local time
Today, 23:53
Joined
Jul 26, 2013
Messages
10,371
You will need to tell us what the field data types are to help you any further, so what types of data are
Finyear
section


Currently access thinks they are numbers?

Where is [COMFY] coming from? Shouldn't it be Me.COMFY if it's on the form?
Where is [TXTXUC] coming from? Shouldn't it be Me.TXTXUC if it's on the form ?

And it might be really helpful to call your controls something sensible like
Text0 change to txtStartDate
Combo4 change to cboEndDate
 

sbaud2003

Member
Local time
Tomorrow, 04:23
Joined
Apr 5, 2020
Messages
178
You will need to tell us what the field data types are to help you any further, so what types of data are
Finyear
section


Currently access thinks they are numbers?

Where is [COMFY] coming from? Shouldn't it be Me.COMFY if it's on the form?
Where is [TXTXUC] coming from? Shouldn't it be Me.TXTXUC if it's on the form ?

And it might be really helpful to call your controls something sensible like
Text0 change to txtStartDate
Combo4 change to cboEndDate

yes they are on the from but the problem lies with the code starting from And [settelment_date] between "#" & [Text0] & "#" and "#" & [Combo4] & "#") if I removed this code the total sum is appearing on the screen but it is not taking between dates with the codes mentiond
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 23:53
Joined
Sep 21, 2011
Messages
14,265
If you put it all in a string and Debug.Print that, it would help me (at least) plus yourself in getting to the bottom of the problem.
Copy and paste the result of the debug.print back here.

Right now, we are just guessing :(
 

Minty

AWF VIP
Local time
Today, 23:53
Joined
Jul 26, 2013
Messages
10,371
Okay so let's try this then;
Code:
Dim sCriteria As String

    sCriteria = "finyear = " & Me.COMFY & " and section = " & Me.TXTUC & " And [settelment_date] between #" & Me.Text0 & "# and #" & Me.Combo4 & "#"
    Debug.Print sCriteria

    Me.tcmp = DSum("Chamt", "total cmp", sCriteria)
 

sbaud2003

Member
Local time
Tomorrow, 04:23
Joined
Apr 5, 2020
Messages
178
yes they are on the from
Okay so let's try this then;
Code:
Dim sCriteria As String

    sCriteria = "finyear = " & Me.COMFY & " and section = " & Me.TXTUC & " And [settelment_date] between #" & Me.Text0 & "# and #" & Me.Combo4 & "#"
    Debug.Print sCriteria

    Me.tcmp = DSum("Chamt", "total cmp", sCriteria)
Thank you sir,,,
but it display the Gross Total and Zero in other cases
between dates are not taken randomly..
 
Last edited:

Minty

AWF VIP
Local time
Today, 23:53
Joined
Jul 26, 2013
Messages
10,371
In the immediate window (Press ctrl & G in the vba editor to see it) you will see what sCriteria is.
Please paste that here.
Don't edit it.

This is an essential debugging technique.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:53
Joined
Feb 19, 2013
Messages
16,607
We're both wrong

gasman
DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] And "[settelment_date] between #" [Text0] & "# and #" & [Combo4] &#")

CJ
DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] " And [settelment_date] between #" & [Text0] & "# and #" & [Combo4] & "#")

missing a & after txttuc
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:53
Joined
Sep 21, 2011
Messages
14,265
We're both wrong

gasman
DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] And "[settelment_date] between #" [Text0] & "# and #" & [Combo4] &#")

CJ
DSum("Chamt", "total cmp", "finyear = " & [COMFY] & " and section = " & [TXTUC] " And [settelment_date] between #" & [Text0] & "# and #" & [Combo4] & "#")

missing a & after txttuc
Well at least I am in good company. :)
 

Minty

AWF VIP
Local time
Today, 23:53
Joined
Jul 26, 2013
Messages
10,371
Well, I'm obviously brilliant 'cos I fixed that in my reply. :p
 

Users who are viewing this thread

Top Bottom