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
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:
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.
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.
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.
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:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.