Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

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

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

CASE 1:
If val(vTimeOut(val(CurrentAlarm))) = 1 Then ' so if yes theres a timeout...
         If TSLS = val(TimeOutDelay(val(CurrentAlarm))) And AlarmState = PlayingThen
            StopAlarm
             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)

CASE 2:
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,

Gizmo
0
GizmoBlaster
Asked:
GizmoBlaster
  • 2
2 Solutions
 
PSSUserCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
GizmoBlasterAuthor Commented:
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!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now