22

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

Urbycoz
  • 7,247
  • 20
  • 70
  • 108

4 Answers4

41

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 3
    In vb**6**, `Dim intID as integer: intID = 40000` will error 100% of the time – Alex K. May 05 '11 at 10:14
  • 1
    Because `3 * 10000` fits in an integer (its < 32767), `4 * 10000` does not – Alex K. May 05 '11 at 10:21
  • @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** ? – AakashM May 05 '11 at 10:48
  • Yes, it's definitely VB6. I'll post a screenshot. – Urbycoz May 05 '11 at 11:08
  • 1
    @Alex- You're right. It's behaving as you say for me now. Not sure what changed. Thanks for your help! – Urbycoz May 05 '11 at 11:21
11

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

You would be best using the Long type here.

trickwallett
  • 2,418
  • 16
  • 15
1

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.

Dim intID as integer
intID = x * 10000

Dim lngID AS Long

lngID = x * CLng(10000)
' if 10000
' whatever you want to be
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
  • 1
    Please explain your answer. You have simply posted the code. – Rahul Vishwakarma Mar 04 '15 at 05:04
  • 2
    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. – Uttam Kumar Roy Mar 05 '15 at 05:42
  • 2
    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. – Contango Apr 08 '15 at 15:41
1

In VB Integer variable range is -32,768 to 32,767 If any variable value is more than this range in your program. Please declare variable in long if your count is more than 32767 then not given any error in your program.

Dim lngid AS long
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103