|
-
Oct 6th, 2007, 01:49 PM
#1
Thread Starter
Addicted Member
overflow in VB6
I don't know what happened but when I try doing any math function in VB6 where the result is greater than 32,767 I get an overflow error. Even in debug if I type ?32767+1 I get this error. This is the same error received if you try to assign a number larger than 32,767 to an integer and it is doing it with all numbers. This just started doing this today. I even tried rebooting. What in the world could cause this to happen?
-
Oct 6th, 2007, 03:25 PM
#2
Junior Member
Re: overflow in VB6
Try ?32768+1 in the debug and you'll see that it works fine.
I assume that VB6 debug window sees the 32767, assigns it to an Integer data type, then you get the overflow. But if debug window sees 32768, it assigns it to a Long data type, so no overflow.
In short, look through your code for variables that are declared As Integer for the problem. Also, add some error trapping so you can step through in debug mode an locate the source of the problem.
-
Oct 6th, 2007, 03:40 PM
#3
Thread Starter
Addicted Member
Re: overflow in VB6
32767+1 was just an example.
If I type into debug, ? 200*200 I get an overflow error. If I decale myVar as double and set myVar = 200*200 I get an overflow error. These are all things I have been doing regularly and now seems to have stopped.
Maybe I am going nuts
-
Oct 6th, 2007, 03:55 PM
#4
Frenzied Member
Re: overflow in VB6
Diffrent Data types can hold diffrent things, you need to know what it supose to hold.
Example:
VB Code:
'0 to 255
Dim Data As Byte
'-32,768 to 32,767
Dim Data As Integer
'-2,147,483,648 to 2,147,483,647
Dim Data As Long
'String is from 0 to somewhere around 2 billion
Dim Data As String
'-922,337,203,685,477.5808 to 922,337,203,685,477.5807
Dim Data As Currency
Change it to double:
This will give you Overflow Error:
VB Code:
Private Sub Form_Load()
Dim MyVar As Integer
MyVar = 32768 + 1
MsgBox MyVar
End Sub
And this will not, it will msgbox you "32769"
VB Code:
Private Sub Form_Load()
Dim MyVar As Double
MyVar = 32768 + 1
MsgBox MyVar
End Sub
And so on.....
VB Code:
Private Sub Form_Load()
Dim MyVar As String
MyVar = Val(200) * Val(200)
MsgBox MyVar
'Will return 40000
End Sub
VB Code:
Private Sub Form_Load()
Dim MyVar As String
MyVar = Val(99999) * Val(99999)
MsgBox MyVar
'Will return 9999800001
End Sub
Last edited by wiz126; Oct 6th, 2007 at 04:22 PM.
-
Oct 6th, 2007, 04:14 PM
#5
Re: overflow in VB6
Code:
Dim x As Long
x = 200
x = x * 200
Debug.Print x
-
Oct 6th, 2007, 04:18 PM
#6
Re: overflow in VB6
if you want to go bigger than long, you can use Currency, which I think is the max for VB6 numerical variables
Delete it. They just clutter threads anyway.
-
Oct 6th, 2007, 04:38 PM
#7
Re: overflow in VB6
 Originally Posted by TheBigB
if you want to go bigger than long, you can use Currency, which I think is the max for VB6 numerical variables
Yep. It can hold 8 Bytes. Use LenB() function next time
-
Oct 6th, 2007, 04:53 PM
#8
Thread Starter
Addicted Member
Re: overflow in VB6
I know what kind of variable to use. I have been using this routine for years. For some reason it stopped working today and I can't even multiply simple numbers in immediate mode. Even typing in ? 87*512 in immediate mode is generating an error.
This should not generate an error in a program but it does. It generates an overflow error.
Dim x as long
x=200*200
However...
Dim x as long
x=200
x=x*x
does not generate an error.
I am trying to multiply Asc(Mid(ID, x, 1)) in which the value is 87 by 512 and am getting an overflow error.
This gives me an overflow error
Dim zz as Double
ID = "DWE"
x=2
zz= Mid Asc(Mid(ID, x, 1)) * (x*256)
This code was working perfect until today. I know I have never had such problems trying to deal with large numbers before.
Back in the 80s when I was first learning about programming on a home computer I would get so upset. I just knew that something was wrong with the machine. It is a rare occasion that it is a machine problem. 99% + of the time it is programmer error. Because of that I hesitate to think it is a machine problem but this is the damnest thing I have ever seen. It was working fine and then stopped.
-
Oct 6th, 2007, 04:57 PM
#9
Re: overflow in VB6
Here's another way:
Code:
Dim x As Long
x = CLng(200) * 200
-
Oct 6th, 2007, 05:03 PM
#10
Thread Starter
Addicted Member
Re: overflow in VB6
 Originally Posted by gavio
Here's another way:
Code:
Dim x As Long
x = CLng(200) * 200
Why doesn't this work?
Dim x as Long
x = 200 * 200
-
Oct 6th, 2007, 05:12 PM
#11
Re: overflow in VB6
Because at least one operand must be in the range of result.
In your case, result is Long (because Long is the first data type that can hold that kind of a number - 40000). Thus, at least one operand must be at least Long - further more, it can be Single or any data type that can hold it:
Code:
x = CSng(200) * 200
VB is multiplying 200 * 200 and makes a temporary variable - or something like that - and doesn't store it into "x" at beginning. And here is the overflow. Let's say, 200 is Integer data type. VB checks both operands and their data types, and takes which one is bigger (DATA TYPE!). Thus, it thinks the result must be Integer. But Integer cannot hold 40000.
I'm not good at this. Hope you get the picture
Last edited by gavio; Oct 6th, 2007 at 05:27 PM.
-
Oct 6th, 2007, 06:06 PM
#12
Thread Starter
Addicted Member
Re: overflow in VB6
 Originally Posted by gavio
Because at least one operand must be in the range of result.
In your case, result is Long (because Long is the first data type that can hold that kind of a number - 40000). Thus, at least one operand must be at least Long - further more, it can be Single or any data type that can hold it:
Code:
x = CSng(200) * 200
VB is multiplying 200 * 200 and makes a temporary variable - or something like that - and doesn't store it into "x" at beginning. And here is the overflow. Let's say, 200 is Integer data type. VB checks both operands and their data types, and takes which one is bigger (DATA TYPE!). Thus, it thinks the result must be Integer. But Integer cannot hold 40000.
I'm not good at this. Hope you get the picture 
I think I am. It still doesn't explain why it was working and now it isn't. I will play with it some more. I don't know what else to do. I know I have done this before in immediate mode and it worked fine.
-
Oct 6th, 2007, 06:24 PM
#13
Re: overflow in VB6
 Originally Posted by sneakers
... I know I have done this before in immediate mode and it worked fine...
Somehow, i doubt it. The immediate mode is none different then any other.
If you concider how VB multiplys (calculates in general), your problem cannot be solved like that - 200 * 200.
Are you sure you weren't using Val()? Like:Because Val() returns Double, thus the result falls in that range:
Code:
?TypeName(Val(200) * 200)
Perhaps you were calling some other function that returned some other data type?
-
Oct 6th, 2007, 10:07 PM
#14
Re: overflow in VB6
Why this piece of code causes overflow?
Code:
Dim x as Long
x = 200 * 200
That is the RHS will be computed first without knowing what the data type of LHS is.
200 and 200 will be treated by default as 2 "Integer" values (because 200 < 32767)
200 * 200 (multiply of 2 "Integer" value) will give out an "Integer" value.
That value is 40000 : this is out of range of an "Integer".
To overcome this problem, write you code like this:
Code:
Dim x as Long
x = 200& * 200&
or just one "&" is enough ("&" is the notation used for "Long" number):
Code:
Dim x as Long
x = 200& * 200
By this way, you explicit tell the compiler: the first number 200 is a "Long" value.
When compute RHS: 200& is a "Long" value, 200 is an "Integer" value.
200& * 200 will give out a value with largest data type that is a "Long".
The LHS x is a "Long" as declared, it can hold the "Long" value 40000&.
Edit Appended:
When using litteral values and you have to deal with larger numbers:
Use Single (upto 3.402823E38) with notation "!", or
Use Double (upto 1.79769313486232E308) with notation "#".
Noted that you should only use larger data type if needed.
Last edited by anhn; Oct 6th, 2007 at 10:36 PM.
-
Oct 7th, 2007, 12:59 AM
#15
Re: overflow in VB6
Simple hard and fast rule:
Integer * Integer = Long
Integer * Long = Long
Long * Long = Double
-
Oct 7th, 2007, 01:59 AM
#16
Re: overflow in VB6
 Originally Posted by randem
Simple hard and fast rule:
Integer * Integer = Long
Integer * Long = Long
Long * Long = Double
What do you mean with those rules?
The second rule is not always correct:
1000 is an "Integer", 2^30 is a "Long" but 1000 * 2^30 is NOT a "Long"
-
Oct 7th, 2007, 08:03 AM
#17
Thread Starter
Addicted Member
Re: overflow in VB6
Clng, Csng and cdbl worked fine. I have no idea what I have been using that worked in the past. Thanks for the help.
-
Oct 10th, 2007, 05:16 AM
#18
Re: overflow in VB6
Yes, a quick typo. To avoid overflows the answer have to be larger than any one of the two multipliers.
-
Oct 10th, 2007, 08:40 AM
#19
Re: overflow in VB6
In VB, if you enter a number without a type indicator (%, &, !, #, etc), if the number is within Integer range, vb treats it as an integer. If number is has a decimal vb treats it as Double.
So 200 is Integer as is 200%, 200& is Long, 200! is Single, 200# is Double. You can verify by ? VarType(200)=vbInteger, VarType(200!)=vbSingle & so on.
Another example emphasizing the point:
? VarType(32767)=vbInteger, VarType(32768)=vbLong
Now when using math, if the result ends up exceeding the max values of the largest datatype in the equation, you will get overflow. Does that help more?
Last edited by LaVolpe; Oct 10th, 2007 at 09:17 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|