Connecting Tech Pros Worldwide Forums | Help | Site Map

Why does DSUM function is returning me a string not a number

Familiar Sight
 
Join Date: Apr 2007
Location: Lebanon
Posts: 211
#1: Nov 17 '08
Expand|Select|Wrap|Line Numbers
  1. nz(DSum("[quantityindetail]*[purchaseprice]-[quantityindetail]*[purchaseprice]*DiscPurchasedetail*0.01","Purchasedetail","[purchaseid]= '" & [Purchaseid] & "'"),0) AS sub_total
  2.  
  3.  nz(DSum("[vatvaluePurchasedetail]","Purchasedetail","[purchaseid]= '" & [Purchaseid] & "'"),0) AS vat_sum 
I am running this two functions in a query.

What I am trying to do is adding

Expand|Select|Wrap|Line Numbers
  1. vat_sum + sub_total as grandtotal
it is working but as string. example 25+5=255

How may I tell the DSUM to return me a number value in place of string ?
any idea it would be appreciated

Thank you in advance

WASSIM S DACCACHE

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#2: Nov 17 '08

re: Why does DSUM function is returning me a string not a number


What Datatypes are vat_sum and sub_total?

Linq ;0)>
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#3: Nov 17 '08

re: Why does DSUM function is returning me a string not a number


Hi Wassim. It's not DSum that returns the string - it's the Nz function. You can convert each Nz string value back to a number using one of the type conversion functions as necessary:

Expand|Select|Wrap|Line Numbers
  1. CCur(Nz(DSum..., 0)) ' convert to currency value
  2. CDbl(Nz(DSum..., 0)) ' convert to double-precision floating point
  3. CLng(Nz(DSum..., 0)) ' convert to long integer
or you can use the Val function to take the numeric value of each string instead

Expand|Select|Wrap|Line Numbers
  1. Val(Nz(DSum..., 0))
However, as Linq has pointed out you should ensure that the type of the variables or fields you are assigning these values to is correct. Access normally does type conversion 'on the fly' - if the result is assigned to a double-precision value, say, then Access would convert the Nz results to floating-point numbers before performing the sum. It suggests you have a text result type somewhere.

-Stewart
Familiar Sight
 
Join Date: Apr 2007
Location: Lebanon
Posts: 211
#4: Nov 18 '08

re: Why does DSUM function is returning me a string not a number


Quote:

Originally Posted by Stewart Ross Inverness

Hi Wassim. It's not DSum that returns the string - it's the Nz function. You can convert each Nz string value back to a number using one of the type conversion functions as necessary:

Expand|Select|Wrap|Line Numbers
  1. CCur(Nz(DSum..., 0)) ' convert to currency value
  2. CDbl(Nz(DSum..., 0)) ' convert to double-precision floating point
  3. CLng(Nz(DSum..., 0)) ' convert to long integer
or you can use the Val function to take the numeric value of each string instead

Expand|Select|Wrap|Line Numbers
  1. Val(Nz(DSum..., 0))
However, as Linq has pointed out you should ensure that the type of the variables or fields you are assigning these values to is correct. Access normally does type conversion 'on the fly' - if the result is assigned to a double-precision value, say, then Access would convert the Nz results to floating-point numbers before performing the sum. It suggests you have a text result type somewhere.

-Stewart

Thank you for your reply. I used the VAL () function and it is converting the string to number.

but I didn't understand why it is replying me a string since i used the dsum function without the NZ () function also the dsum function is doing the sum of fields that datatypes are double.

regards,
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#5: Nov 19 '08

re: Why does DSUM function is returning me a string not a number


It's just one of many quirks in Access! When used in a query, Nz() always returns a string!

Linq ;0)>
Reply


Similar Microsoft Access / VBA bytes