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:

enter image description here

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.

Share:
64,268

Related videos on Youtube

Urbycoz
Author by

Urbycoz

Updated on December 29, 2021

Comments

  • Urbycoz
    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 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

  • Alex K.
    Alex K. about 13 years
    In vb6, Dim intID as integer: intID = 40000 will error 100% of the time
  • Alex K.
    Alex K. about 13 years
    Because 3 * 10000 fits in an integer (its < 32767), 4 * 10000 does not
  • AakashM
    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
    Urbycoz about 13 years
    Yes, it's definitely VB6. I'll post a screenshot.
  • Urbycoz
    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
    Rahul Vishwakarma over 9 years
    Please explain your answer. You have simply posted the code.
  • Uttam Kumar Roy
    Uttam Kumar Roy over 9 years
    In 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
    Contango about 9 years
    Hi @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.