I am trying to set an integer value as such:
Dim intID as integer
intID = x * 10000
This works ok when x
is 3 or less. But when x
is 4, this gives me the error:
run-time error 6 Overflow
I don't understand why this is. I can set intID
to 40000 directly without any problems, so it's obviously capable of storing large numbers.
To correct this error Make sure that results of assignments, calculations, and data type conversions are not too large to be represented within the range of variables allowed for that type of value, and assign the value to a variable of a type that can hold a larger range of values, if necessary.
In languages where integer overflow can occur, you can reduce its likelihood by using larger integer types, like Java's long or C's long long int. If you need to store something even bigger, there are libraries built to handle arbitrarily large numbers.
An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold.
It can store integer numbers in the range of -2,147,483,648 to 2,147,483,647 and attempting to store a value outside of that range will result in runtime error 6: Overflow.
You *cannot set a vb6 integer to 40000 as they are signed 16 bit numbers so +32767 is the maximum.
Long
is the 32 bit type.
However as a caveat, if you were to:
Dim lngID As Long
lngID = 4 * 10000
You would still get an overflow as literal numbers default to Integer, to correct that just type one as long with &
or cast one as long using CLng()
:
Dim lngID As Long
lngID = 4 * 10000&
lngID = 4 * CLng(10000)
Update:
in VB6, the Integer type is a whole number which ranges from -32768 to 32767.
You would be best using the Long
type here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With