VB6 Overflow error

Discussion in 'Programmer's Corner' started by Engr, Aug 21, 2012.

  1. Engr

    Thread Starter Member

    Mar 17, 2010
    114
    5
    Hi,

    I need some help with my VB6 code.

    I have a variable in my code set as long. As what I understand in programming, if the variable is set as integer it can only handle up to 32000 numbers and if it is set to long it can handle up to 2000000 numbers. My variable is equal to the product of 188 * 366 (variable = 188*366). Even if it is set as long I am still getting an "over flow" error but if I hardcode the variable = 1000000 the "over flow" error is not showing up. 1000000 is greater than 188*360 (= 67680) but I am not getting the over flow error in 1000000. Can anyone help me with this?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Declare the values as long constants or variables first, e.g.

    DIM L188 as Long

    L188 = 188
     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Make sure that numbers used in calculations that are coerced into integers do not have results larger than integers. One way to avoid this is to assign each value to a variable that is declared as a Long Integer, this should get you by this error.....

    Code ( (Unknown Language)):
    1.  
    2. Dim product As Long
    3. Dim first_val As Long
    4. Dim second_val As Long
    5. first_val = 188
    6. second_val = 360
    7.  
    8. product = (first_val * second_val)
    9. Text1.Text = product
    10.  
     
  4. Engr

    Thread Starter Member

    Mar 17, 2010
    114
    5
    Hi BMorse,

    I already tried that approach and still encountered the same problem. All variables set as integer has been changed to long and still the over flow error occurs.
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    I tried it the same way as you had it at first, i got the error also, but when i declared those variables and assigned them the values and the error went away. . . If you are still having issues, then you need to post your code so we can see what else may be causing it.
     
  6. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    any reason why troublesome piece of code is not posted?
     
    BMorse likes this.
  7. Engr

    Thread Starter Member

    Mar 17, 2010
    114
    5
    I didnt post the code since it is very long but I think I have already found the solution. I used VB net and the over flow error went away.
     
Loading...