473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Concatenate (?) versus sum (+) clarification

TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I was trying to look at this post Summing Fields if not null
and to test it I created an unbound form, with 4 unbound text fields, and a calculated field, with the control
Expand|Select|Wrap|Line Numbers
  1. source:=nz([Tekst1];0)+nz([Tekst2];0)+nz([Tekst3];0)+nz([Tekst4];0)
I was a bit surprised to see that when i wrote 1 in the first field 2 in the second and so on, that my calculated field came out as 1234 instead of 10 as expected.

Im guessing that access not knowing better since the fields were all unbound figured I wanted to concatenate it (which seems dumb when we have an operator spefically for that purpose the &).

Does anyone have any other insight to offer on this, and how to avoid access using the + to concatenate fields?
Sep 10 '10 #1
5 6450
NeoPa
32,556 Recognized Expert Moderator MVP
Absolutely Smiley :)

The + character as a concatenator is not redundant. It is not the same as the & (See Using "&" and "+" in WHERE Clause).

To avoid any ambiguity it is necessary to convert (at least) the first value to a numeric value. Use something like :
Expand|Select|Wrap|Line Numbers
  1. Val(Nz([Field1],0))
Now you've pointed that out though, I'll need to update my own suggestion in that thread to handle this little side-effect :(
Sep 10 '10 #2
Stewart Ross
2,545 Recognized Expert Moderator Specialist
In object-oriented terms the '+' operator is overloaded when applied to strings to provide string concatenation. This is not unusual; the operator is already overloaded to provide addition of different numeric types transparently, because at a low level addition of floating-point values is not the same as addition of long integers, is not the same as addition of currency types, and so on.

In Access the use of '+' is fine if the underlying type to which the operator is being applied is clear. Unbound text boxes, however, will always be treated as string values - just as the value returned by the use of Nz in a query will always be treated as a text value, not a variant. When the underlying type cannot be known by the query engine then text will be assumed, as all displayable types can at least be stored in a text field.

The solution when working with expressions involving Nz in queries or where additions of unbound textbox values is involved is to explicitly typecast them, as NeoPa suggested - and particularly if using Nz, which returns a variant value that the Access query engine will interpret as text. This leads to somewhat clumsy expressions such as

Expand|Select|Wrap|Line Numbers
  1. =CLng(Nz(Sum([A Numeric Value]), 0))
and

Expand|Select|Wrap|Line Numbers
  1. =CLng(Nz([textbox1], 0)) + CLng(Nz([textbox2], 0)) +  CLng (Nz ([textbox3], 0))
The use of '+' to mean string concatenation came long before the provision of a separate string concatenation operator (the '&'), and indeed it is itself an overloaded operator, used with a following H to indicate hexadecimal values, for instance

Expand|Select|Wrap|Line Numbers
  1. Const cHexChar = &H25AA
Far from being too dumb it's another example of Access (indeed VB/VBA related systems in general) being too clever!

-Stewart
Sep 11 '10 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Good answers both of you, but I had to choose just one. Thank you both!
Sep 16 '10 #4
NeoPa
32,556 Recognized Expert Moderator MVP
No foul.

If mine was good, Stewart's was excellent. I'd have chosen his :D
Sep 16 '10 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Thank you both for your kind comments!

:)

-Stewart
Sep 16 '10 #6

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

Similar topics

2
by: Elliot Rodriguez | last post by:
As I continue to read more about the benefits of database querying using ADO.NET, I am having a more difficult time distinguishing what the best approach to data retrieval is anymore. When...
13
by: Giggle Girl | last post by:
Hi there, I need to use a background image in a TR that does NOT restart everytime it hits a TD. Can it be done? Specifically, if you set a background image for an entier table, now mater how...
1
by: Krysa | last post by:
Access 2K, DAO, split front end and back end; back end on server. Regarding max of 255 concurrent users. Is it really users, or connections? How would a user have more than one connection to same...
5
by: Jim Carlock | last post by:
I'm looking to use an image of letters and numbers, whereby I need a way to extract each letter and number to create random strings, then combine/conjoin/meld/mesh/merge/unify/unite the images...
6
by: Sheldon | last post by:
Hi, I am trying to build a large array using concatenate function in python. So as I loop over the number of arrays, of which there are 12 (4 down and 3 across), I create 3 long arrays by...
14
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
2
by: exapplerep | last post by:
I've seen how to use VBA code to concatenate two fields into a third by using an expression in the "After Update" property in fields 1 & 2. field3 = field1 + field2 The above code would go...
4
by: Dan | last post by:
Hi all, I am creating a search table where the keywords field is made up of several text fields and this is causing me some problems. I can concatentate the text ok but i can't seem to concatenate...
12
by: parth | last post by:
Hi I want to achieve the following transformation of data using a stored procedure. Source col1 col2(varchar) -------------------------
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.