Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 overflow error with large integers

Tags:

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.

enter image description here

like image 685
Urbycoz Avatar asked May 05 '11 09:05

Urbycoz


People also ask

How do I fix Visual Basic overflow error?

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.

How do you solve integer overflow problems?

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.

Which type of overflow happens when INT exceeds max value?

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.

How high can integers go in VBA?

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.


2 Answers

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:

enter image description here

like image 97
Alex K. Avatar answered Oct 16 '22 12:10

Alex K.


in VB6, the Integer type is a whole number which ranges from -32768 to 32767.

You would be best using the Long type here.

like image 42
trickwallett Avatar answered Oct 16 '22 11:10

trickwallett