Results 1 to 19 of 19

Thread: overflow in VB6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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?

  2. #2
    Junior Member
    Join Date
    May 2006
    Posts
    31

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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

  4. #4
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: overflow in VB6

    Diffrent Data types can hold diffrent things, you need to know what it supose to hold.

    Example:
    VB Code:
    1. '0 to 255
    2. Dim Data As Byte
    3. '-32,768 to 32,767
    4. Dim Data As Integer
    5. '-2,147,483,648 to 2,147,483,647
    6. Dim Data As Long
    7. 'String is from 0 to somewhere around 2 billion
    8. Dim Data As String
    9. '-922,337,203,685,477.5808 to 922,337,203,685,477.5807
    10. Dim Data As Currency

    Change it to double:

    This will give you Overflow Error:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim MyVar As Integer
    3.     MyVar = 32768 + 1
    4.     MsgBox MyVar
    5. End Sub

    And this will not, it will msgbox you "32769"

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim MyVar As Double
    3.     MyVar = 32768 + 1
    4.     MsgBox MyVar
    5. End Sub




    And so on.....

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim MyVar As String
    3.     MyVar = Val(200) * Val(200)
    4.     MsgBox MyVar
    5.     'Will return 40000
    6. End Sub

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim MyVar As String
    3.     MyVar = Val(99999) * Val(99999)
    4.     MsgBox MyVar
    5.     'Will return 9999800001
    6. End Sub
    Last edited by wiz126; Oct 6th, 2007 at 04:22 PM.
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  5. #5

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  7. #7
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: overflow in VB6

    Quote 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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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.

  9. #9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    Re: overflow in VB6

    Quote 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

  11. #11
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    Re: overflow in VB6

    Quote 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.

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: overflow in VB6

    Quote 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:
    Code:
    ?Val(200) * 200
    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?

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: overflow in VB6

    Simple hard and fast rule:

    Integer * Integer = Long
    Integer * Long = Long
    Long * Long = Double

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: overflow in VB6

    Quote 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"

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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.

  18. #18
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: overflow in VB6

    Yes, a quick typo. To avoid overflows the answer have to be larger than any one of the two multipliers.

  19. #19
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
  •  



Click Here to Expand Forum to Full Width