Run time error '6' Overflow (1 Viewer)

gsrajan

Registered User.
Local time
Today, 12:47
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
 

Minty

AWF VIP
Local time
Today, 17:47
Joined
Jul 26, 2013
Messages
10,371
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.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Thank you for your reply. I tried but still getting same error message. Thank you.
 

Minty

AWF VIP
Local time
Today, 17:47
Joined
Jul 26, 2013
Messages
10,371
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)
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Values : 0 0
Values : 0 0
Values : 0 0
Values : 0 0

Thank you.
 

Minty

AWF VIP
Local time
Today, 17:47
Joined
Jul 26, 2013
Messages
10,371
So there you go - 0 / 0 is causing your overflow.

Whatever values you thought you where getting aren't being set.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
Please let me know how to fix this. Thank you for your time.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:47
Joined
May 21, 2018
Messages
8,543
If then check if the divisor is 0
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
I have 100 and 25 in Total and Used text boxes respectively. Getting same error.
 

cheekybuddha

AWF VIP
Local time
Today, 17:47
Joined
Jul 21, 2014
Messages
2,280
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:47
Joined
May 21, 2018
Messages
8,543
Sorry you probably cannot make percentage a long, needs to be double.
 

Minty

AWF VIP
Local time
Today, 17:47
Joined
Jul 26, 2013
Messages
10,371
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.
 

gsrajan

Registered User.
Local time
Today, 12:47
Joined
Apr 22, 2014
Messages
227
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

Top Bottom