Concatenation not addition?

B

Barry

Good evening all:
I am trying to add numbers together and they are being concatenated rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry
 
J

John W. Vinson

Good evening all:
I am trying to add numbers together and they are being concatenated rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry

This will happen if the values are Text. If these are unbound textboxes on a
form, they'll be treated as text (if they are fields in a table, they
shouldn't unless of course they ARE of Text datatype; note that a Text field
can contain numbers!)

You might try

= Val([field1]) + Val([field2])

as the control source of a textbox on a form, or

TheSum: Val([field1]) + Val([field2])

as a calculated field in a query.
 
G

Gina Whipp

Arvin,

I could be wrong but I don't think that will work on text fields and the
only time Access doesn't add is if they are text fields, it concates (is
that a word??? okay well, you know what I mean).

--
Gina Whipp
2010 Microsoft MVP (Access)

"I feel I have been denied critical, need to know, information!" - Tremors
II

http://www.regina-whipp.com/index_files/TipList.htm

Arvin Meyer said:
Try:

= Sum([Field1]+[Field2])
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Barry said:
Good evening all:
I am trying to add numbers together and they are being concatenated
rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry
 
A

Arvin Meyer [MVP]

Hi Gina,

He states that "both numbers are numeric"

You are correct. He will get an error if they aren't, but I'm giving him the
benefit of the doubt until he gets the error.

BTW, the word you were looking for is concatenate.
--
Arvin

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Gina Whipp said:
Arvin,

I could be wrong but I don't think that will work on text fields and the
only time Access doesn't add is if they are text fields, it concates (is
that a word??? okay well, you know what I mean).

--
Gina Whipp
2010 Microsoft MVP (Access)

"I feel I have been denied critical, need to know, information!" - Tremors
II

http://www.regina-whipp.com/index_files/TipList.htm

Arvin Meyer said:
Try:

= Sum([Field1]+[Field2])
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Barry said:
Good evening all:
I am trying to add numbers together and they are being concatenated
rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry
 
G

Gina Whipp

Arvin,

True he does state that AND he is correct, numbers are numeric but fields
are not <g> and I find people often get that confused. They think storing a
number in a text field *makes* the field numeric.

<Oh Dear> Well, I do have the David's typist sister, I see she isn't much
better at spelling! Thanks!

--
Gina Whipp
2010 Microsoft MVP (Access)

"I feel I have been denied critical, need to know, information!" - Tremors
II

http://www.regina-whipp.com/index_files/TipList.htm

Arvin Meyer said:
Hi Gina,

He states that "both numbers are numeric"

You are correct. He will get an error if they aren't, but I'm giving him
the benefit of the doubt until he gets the error.

BTW, the word you were looking for is concatenate.
--
Arvin

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Gina Whipp said:
Arvin,

I could be wrong but I don't think that will work on text fields and the
only time Access doesn't add is if they are text fields, it concates (is
that a word??? okay well, you know what I mean).

--
Gina Whipp
2010 Microsoft MVP (Access)

"I feel I have been denied critical, need to know, information!" -
Tremors II

http://www.regina-whipp.com/index_files/TipList.htm

Arvin Meyer said:
Try:

= Sum([Field1]+[Field2])
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Good evening all:
I am trying to add numbers together and they are being concatenated
rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry
 
B

Barry

John:
Yes indeed they are unbound fields. I am using them to sum things up and
give a visual of prices, payments, amounts due etc. I was not aware that the
fields were treated as such. Thanks to all for the help.
Sincerely,
Barry

John W. Vinson said:
Good evening all:
I am trying to add numbers together and they are being concatenated rather
than summed. Both numbers are numeric.
Example:
Field1: 5
Field2: 10
Field1 + Field2 = 510

Does anyone have an explanation for this behavior?
Thanks,
Barry

This will happen if the values are Text. If these are unbound textboxes on a
form, they'll be treated as text (if they are fields in a table, they
shouldn't unless of course they ARE of Text datatype; note that a Text field
can contain numbers!)

You might try

= Val([field1]) + Val([field2])

as the control source of a textbox on a form, or

TheSum: Val([field1]) + Val([field2])

as a calculated field in a query.
 
B

Barry

Thanks,
It appears that CLng or Val both work nicely. I was unaware that the
unbound text boxes would be treated as text.
Thanks for the help.
Barry
 
D

David W. Fenton

He states that "both numbers are numeric"

If they were numeric, they wouldn't have concatenated with the +
operator (which is the concatenation operator that propagates Null
*if* it's used with non-numeric data).
 
D

David W. Fenton

It appears that CLng or Val both work nicely.

Use Val() and not CLng(), because CLng() will return only the
Integer part of the values. When all you want to do is coerce
strings into numbers, Val() is the best way to do it. CLng() coerces
a string to a valid long integer value (i.e., no decimal part),
which means it does more than just convert the string to a numeric
value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top