Getting Error 6 (Overflow) on Yet Simple Code With No Possible Overflow!

Posted on 2005-04-28
Last Modified: 2010-05-02
I hate it when VB gives me absurd error messages that just can't apply according to VB's definition of the error message.

In the following 2 cases, I get error 6 "Overflow":

If val(vTimeOut(val(CurrentAlarm))) = 1 Then ' so if yes theres a timeout...
         If TSLS = val(TimeOutDelay(val(CurrentAlarm))) And AlarmState = PlayingThen
             Call AddLabMess("The alarm has timed out", "regular")
         End If
End If
(the first line is the one that spawns the error - i number my lines for better troubleshooting)

If Retard > -1 Then Retard = Retard - 1

In both cases, this is just a if-then, there is not even a variable assignment so how can there be an overflow????

especially when help describes overflow this way (my angry comments in capitals):
"An overflow results when you try to make an assignment (I DON'T DO ANY ASSIGNMENT...) that exceeds the limitations of the target of the assignment.  This error has the following causes and solutions:  

The result of an assignment, calculation, or data type conversion is too large to be represented within the range of values allowed for that type of variable. (YEAH RIGHT, NUMBER "1" IS TOO LARGE)

Assign the value to a variable of a type that can hold a larger range of values. (I DON'T MAKE ASSIGNMENTS ON THESE LINES)

An assignment to a property exceeds the maximum value the property can accept. (YET AGAIN...)

Make sure your assignment fits the range for the property to which it is made."

Hope someone can make sense of it,

Thanks guys,

Question by:GizmoBlaster
    LVL 10

    Accepted Solution

    In the first case, you are kind of making an assignment.
    You are assigning the "val" of CurrentAlarm to a space in memory and also the "val" of vTimeOut(val(CurrentAlarm)).

    What is contained in CurrentAlarm and vTimeOut at the point you get an error?

    For the second point, you are assigning Retard - 1 to Retard.

    What is Retard defined as? If it's a byte, then it can't be negative.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    In your statement:

        If val(vTimeOut(val(CurrentAlarm))) = 1 Then ' so if yes theres a timeout...

    What is the datatype and/or value of CurrentAlarm?  The Val() function picks the best return datatype based upon it's input.  You then pass the return value from Val() into your vTimeOut() function.  What is the datatype for the parameter to this function?  If the return datatype and/or value from the previous call to Val() exceeds the parameter type for vTimeOut() then you will get this error because you ARE making an assignment by passing a value into a function!  Below is a simple example of how this can happen:

    Option Explicit

    Private Sub Command1_Click()
        Dim y As String, z As Long
        y = "60000" ' this is larger than an Integer can hold so Val() below will return a Long
        If foo(Val(y)) = 1 Then ' this will cause an overflow error!
            Debug.Print "There IS an assignment happening above..."
        End If
    End Sub

    Private Function foo(ByVal x As Integer) As Integer
        foo = x
    End Function

    Author Comment

    These are 2 interesting comments guys, didn't realize that using val was being an assignment. Will revisit my code and either close that question or provide more details, thanks!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Most likely the overflow is occurring because there is an assignment from the return of val(CurrentAlarm) to the parameter of vTimeOut().  Look at the value of CurrentAlarm (and what data type could hold that) versus the data type of the parameter to vTimeOut().

    Study the small example I gave...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now