VB6 overflow error with large integers
64,268
Solution 1
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:
Solution 2
in VB6, the Integer type is a whole number which ranges from -32768 to 32767.
You would be best using the Long
type here.
Related videos on Youtube
Author by
Urbycoz
Updated on December 29, 2021Comments
-
Urbycoz over 2 years
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 whenx
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. -
Alex K. about 13 yearsIn vb6,
Dim intID as integer: intID = 40000
will error 100% of the time -
Alex K. about 13 yearsBecause
3 * 10000
fits in an integer (its < 32767),4 * 10000
does not -
AakashM about 13 years@Urbycoz the great majority of your questions have been about VB.NET . For the avoidance of any doubt, could you confirm that you are definitely seeing this behaviour in VB6 ?
-
Urbycoz about 13 yearsYes, it's definitely VB6. I'll post a screenshot.
-
Urbycoz about 13 years@Alex- You're right. It's behaving as you say for me now. Not sure what changed. Thanks for your help!
-
Rahul Vishwakarma over 9 yearsPlease explain your answer. You have simply posted the code.
-
Uttam Kumar Roy over 9 yearsIn VB Integer variable range is -32,768 to 32,767 If any variable value is more than this range in your program,you have to declare Data type Long instead of Integer.Thanks.
-
Contango about 9 yearsHi @Always Beginner, welcome to Stack Overflow. You are right, the correct way to solve this is to use a long instead of an int. Thank you for providing this answer.