Run time error '6' Overflow

gsrajan

Registered User.
Local time
Today, 19:56
Joined
Apr 22, 2014
Messages
227
Please let me know what I am doing wrong in this code. I am getting overflow error. Thank you for your help.

Dim myPrecentge As Long
Dim Total As Double
Dim Used As Double

txtTotal = Total
txtUsed = Used

myPrecentge = Round((Used / Total) * 100,0)

If myPrecentge >= 15 Then
MsgBox Some Message

Else
MsgBox Some Message
End If
End Sub
 
Check your values for Total and Used?
Code:
Dim myPrecentge As Long
Dim Total As Double
Dim Used As Double

Me.txtTotal = Total
Me.txtUsed = Used

Debug.Print "Values : " , Total , Used

myPrecentge = Round((Used / Total) * 100,0)

If myPrecentge >= 15 Then
MsgBox Some Message

Else
MsgBox Some Message
End If
End Sub
And assuming txtTotal and TxtUsed are form controls better to refer to them using Me.
 
Thank you for your reply. I tried but still getting same error message. Thank you.
 
Thank you for your reply. I tried but still getting same error message. Thank you.
So what did the debug show you? What values were being shown.
Open the immediate window in the VBA editor (press ctrl & G together)
 
Values : 0 0
Values : 0 0
Values : 0 0
Values : 0 0

Thank you.
 
So there you go - 0 / 0 is causing your overflow.

Whatever values you thought you where getting aren't being set.
 
Please let me know how to fix this. Thank you for your time.
 
If then check if the divisor is 0
 
I have 100 and 25 in Total and Used text boxes respectively. Getting same error.
 
You are setting your textboxes to the variable values (default 0) rather than setting your variable values to the textbox values!! 😖
Code:
  Dim myPrecentge As Long
  Dim Total As Double
  Dim Used As Double

  Total = Me.txtTotal     ' <-- This was wrong way round!
  Used = Me.txtUsed       ' <-- This was wrong way round!

  It Total > 0 Then
    myPrecentge = Round((Used / Total) * 100,0)
    If myPrecentge >= 15 Then
      MsgBox Some Message
    Else
      MsgBox Some Message
    End If
  End If
 
Sorry you probably cannot make percentage a long, needs to be double.
 
You are setting your textboxes to the variable values (default 0) rather than setting your variable values to the textbox values!! 😖

Sometimes my stupid is extra strong. Today is one of those days.
 
Cheekybudha, thank you so much. It works fine. (My stupidity is always strong!) Thank you everyone for your time.
 

Users who are viewing this thread

Back
Top Bottom