Results 1 to 5 of 5

Thread: [RESOLVED] error 6 overflow

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    77

    Resolved [RESOLVED] error 6 overflow

    i'm getting the overflow error for the caltest variable and also for the calc variable, the function, for these numbers, in order 1 through 5
    27340, 25660, 30490, 28690, 25550, that are passed to the function
    Code:
    Function calc(date1Reach As Integer, date2Reach As Integer, date3Reach As Integer, date4Reach As Integer, date5Reach As Integer) As Double
    Dim calctest As Double
    On Error GoTo err_trap
    calctest = (date1Reach + date2Reach + date3Reach + date4Reach) / 4
    calc = (((date1Reach + date2Reach + date3Reach + date4Reach) / 4) _
             - ((date2Reach + date3Reach + date4Reach + date5Reach) / 4)) _
             / ((date2Reach + date3Reach + date4Reach + date5Reach) / 4)
    i dont see how (27340+25660+30490+ 28690)/4 could cause an overflow for a double? and as well for numbers in this range how it could throw an overflow for the longer calculation for the value of the function, calc?

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

    Re: error 6 overflow

    Most likely (date1Reach + date2Reach + date3Reach + date4Reach) when added exceeds max integer value. Maybe same reason why calc is overflowing.

    One way this can be avoided is by forcing a non-Integer value into the equation:
    (0# + date1Reach + date2Reach + date3Reach + date4Reach)

    Because 0#, which is a Double, is used in the equation, the entire equation should be interpreted as Double vs Integer. Worth a try
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: error 6 overflow

    As implied by LaVolpe, the problem is the data types that VB uses while doing the calculation - for some reason it will use the same data type as the elements (so in this case Integer) rather than either a larger one (like Long) or the one that the result will be stored in (in this case Double).

    In addition to the solution LaVolpe posted, you can convert one of the elements to a larger data type:
    Code:
    calctest = (CLng(date1Reach) + date2Reach + date3Reach + date4Reach) / 4

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    77

    Re: error 6 overflow

    thanks very much for the help, that was a weird situation. In the end I changed the function variable list to (date1reach as long, etc) and that solved problem as well.
    the numbers originally come from an access db where they are stored as longs, so it was my dumb mistake to cast them as integers in the function call.
    from a vb architecture point of view, in retrospect, i guess it makes sense to cast an interim result (within parens) into the largest variable type included in the group, some type of bucket has to be chosen by the program behind the scenes.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: error 6 overflow

    Now that we've helped you, you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that easily by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post.

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