Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding two integer values 1

Status
Not open for further replies.

Syerston

Programmer
Jun 2, 2001
142
GB
When I try to add the two values assinged to integers, VBA concatenates them instead. Would anyone know how to stop this happening. John
 
How ru doing the calculation? If u use the '+' sign, it should work. Hope this is ok. If not, just let me know.

Nick (Everton Rool OK!)
 
Would have been better if you had included the code in your question. Notwithstanding this, it seems to me that the two "numbers" assigned to integers are in fact string variables or variants.

For example:

i = "3"
j = "4"
k = i + j 'will result in "34" if i and j are not declared (or are declared as variants or strings)

Where as:

dim i, j as integer
i = "3"
j = "4"
k = i + j 'will result in 7, as the "3" and "4" are converted to integers on assignment to i and j

Thus, make sure that your operands are declared as numeric data types before applying the + operator to them.

The following will also do the trick, where the i and j operands have not been declared.

i = "3"
j = "4"
k = val(i) + val(j) ' will result in 7

Hope that this helps,

Cheers,
Steve
 
Steve the Val function worked fine.

But I am not quite sure why it had to be used in the first place.

The three variables were declared as integers with the values from two unbound text boxes assinged to two of the variables with the third being used for the total:

iTotal = iPostIssue + iPreIssue
John
 
John,

Here's what I think is happening (and it is a beauty!)
Your code presumably looks something like this:

Dim i, j, k As Integer
i = "12" 'simulates Forms text control
j = "15" 'ditto
k = i + j
Debug.Print k

This little gem yields numeric 1215 as the value of k.

Why? You ask. The reason I believe has to do with the Dim statement.

Dim i, j, k As Integer is NOT declaring all three variables as integers. To do this, you would have to code it as:

dim i as integer, j as integer, k as integer

or

dim i as integer
dim j as integer
dim k as integer

I think What Dim i, j, k is being interpreted as is:

Dim i as Variant, j as Variant, k as integer

Whilst you can declare multiple variables in a Dim statement, they each assume type Variant unless the type is associates to each variable declared.

This obviously only casts k as an integer AFTER concatenating the two variant (ie. which are cast as strings as they input as string fields).

Get the drift.

I'd suggest that the VB language is most at fault here. Most people would like the Dim i, j, k as Integer to be interpreted exactly as you and I (mistakenly) have interpreted it ; ie. all three variables as integers. The VB interpreter thinks otherwise.

Hope that this solves the problem.

Cheers,
Steve
 
Thanks a lot Steve.

That is exactly what I have been doing, and I never once realised that VB was doing that in the background.
John
 
I will have to give u a star 4 that. I ALWAYS use the syntax
dim i as integer
dim iCount as integer etc etc

I do this out of habbit. But strangely enough, i did it the other way today, cause i was being lazy

dim i, j, k as integer, assuming that all vars would be declared as ints. I am sure that u could do this in Paradox, because i have deffo used some language where u can set up vars this way.

Anyway, cheers m8ee
Hope this is ok. If not, just let me know.

Nick (Everton Rool OK!)
 
Thanks for the star Nick. Agree that its a good idea to declare one variable per line anyway. It also allows you to put an explicit comment after each declaration.

Cheers,
Steve
 
Just as an FYI this is the way VB(A) started working when M/S switched from "Access Basic" in V.2 to VBA in Access/95..it catches a lot of people off guard sometimes.

There are two ways to argue with a woman - neither one works.
Another free Access forum:
More Access stuff at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top