Results 1 to 2 of 2
2004-05-01, 04:40 #1
- Join Date
- Feb 2001
- Thanked 1 Time in 1 Post
Odd calculation behaviour (2000sp3)
I receive an overflow error on this statement
if bVar2-bVar3<0 (all vars are byte). If I declare either Var2 or Var3 as variant (or even integer) instead of byte, I don't receive this error, however.
I assume it's something to do with the way intermediate variables are set up, that although nowhere in this statement does a byte variable have a negative number assigned to it, Access assumes all intermediates will also be bytes.
Does this apply generally to all expressions? Do all intermediaries have to be within the limits of the datatype of the "biggest" datatype variable in that expression?
2004-05-01, 09:44 #2
- Join Date
- Mar 2002
- Thanked 28 Times in 28 Posts
Re: Odd calculation behaviour (2000sp3)
>> Do all intermediaries have to be within the limits of the datatype of the "biggest" datatype variable in that expression?
Yes, that is correct.
The values of the Byte data type range from 0 to 255. Since bVar2 and bVar3 are both bytes, bVar2 - bVar3 is evaluated as a byte too, resulting in an overflow error. To get around this, cast at least one of them as an integer or long integer, for example bVar2 - CLng(bVar3) or CLng(bVar2) - cLng(bVar3).