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

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

In vb6, Dim intID as integer: intID = 40000 will error 100% of the time
Because 3 * 10000 fits in an integer (its < 32767), 4 * 10000 does not
@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 ?
Yes, it's definitely VB6. I'll post a screenshot.
@Alex- You're right. It's behaving as you say for me now. Not sure what changed. Thanks for your help!
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.

Comments

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

3 Comments

Please explain your answer. You have simply posted the code.
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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.