0

I am getting Runtime error 6 Overflow when iam executing my vb6 application. i am getting this error on this line.

Sections("Section5").Controls("lblcap").Caption = Format(((lTotOperMins - KPIDown) / lTotOperMins) * 100, "#0.00")

the values lTotOperMins=0 , KPIDown=0 , and Data Types as Long what is wrong with this code. Suggest me, i am not familiar to VB6.

3
  • Not sure why you're getting an overflow error, but the problem is lTotOperMins = 0. You cannot divide by 0. Commented May 8, 2014 at 4:06
  • Yes,But what is the solution when i need to pass that values ? Commented May 8, 2014 at 4:09
  • 2
    If lTotOperMins = 0 then Sections("Section5").Controls("lblcap").Caption = "0"? Commented May 8, 2014 at 4:17

1 Answer 1

3

As jac said, it's because you're not checking first for a division by zero. I know that seems misleading because you're not getting that error, you're getting an Overflow.

As to why you're getting that instead of a division by zero is not something I'm awake enough to think deeply about, but simply checking first fixes the overflow. Here is an example resulting in an overflow followed by a working example with no errors:

Edit:### see very bottom showing how dividing TWO zeros results in an overflow, if one number is not 0 then it becomes a division by 0 error. Code at bottom.

Overflow error:

Image showing where overflow is occurring (can't upload, not enough reputation):

Click to view the image

Private Sub Form_Load()
  Dim lTotOperMins As Long, KPIDown As Long
  Dim lonDifference As Long

  lTotOperMins = 0
  KPIDown = 0

  ' Prevent / by 0
  lonDifference = lTotOperMins - KPIDown

  'If 0 = lonDifference Then
    'Label1.Caption = Format(0, "#0.00")
  'Else
    Label1.Caption = Format(((lonDifference) / lTotOperMins) * 100, "#0.00")
  'End If

End Sub

Working example:

Private Sub Form_Load()
  Dim lTotOperMins As Long, KPIDown As Long
  Dim lonDifference As Long

  lTotOperMins = 0
  KPIDown = 0

  ' Prevent / by 0
  lonDifference = lTotOperMins - KPIDown

  If 0 = lonDifference Then
    Label1.Caption = "0.00"
  Else
    Label1.Caption = Format(((lonDifference) / lTotOperMins) * 100, "#0.00")
  End If

End Sub

Dividing two zeros results in overflow error

Private Sub Form_Load()
  Dim a As Long, b As Long

  a = 0
  b = 0

  Debug.Print a / b
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

@user3463529: You should mark Daniel's answer as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.