UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Mod Operator & Divide By Zero Error, Access 2016    
 
   
USMCdBA
post Nov 20 2017, 12:00 PM
Post#1



Posts: 158
Joined: 11-December 02
From: Charleston, SC


Hello UA,
Current project requires entry of time in hours & minutes where the minutes are captured in 1/4 hour increments ( .00 ; .25 ; .50 ; .75)" only. As an example for 5 3/4 hours, the user would enter 5.75 . The default value for the field is 0.00 and at the form control, a text box, if the user simply enters a whole number with no decimal point, it needs to append .00 to the entry. Setting the form control format to fixed with 2 decimal places should handle the latter requirement.

The Tooltip text is:
"Enter time in 15 minute increments ( .00 ; .25 ; .50 ; .75 ) ONLY please."

To ensure I get one of those 4 choices, I determined that any acceptable entry can be evenly divided by 0.25 with no remainder. Obviously this describes the use of the Mod Operator (I thought). I set it up as shown below --I think it should work, but I keep getting a divide by zero error.
EHW is the textbox collecting the time entry.


Private Sub EHW_AfterUpdate()
Dim ModTime As Integer

ModTime = ((Me.EHW.Value) Mod (0.25))

If ModTime <> 0 Then
Me.EHW.BackColor = vbRed
Me.EHW.Value = Null
Me.EHW.SetFocus
Else
Me.EHW.BackColor = vbWhite

End If

End Sub


The Quick watch is showing the math is right, but not a division by zero. ??? I am stuck and hopefully, someone can help.

Thanks much!
KDP

--------------------
KDP
Charleston, SC -- USA
Bless those who don't need everything immediately.
Go to the top of the page
 
theDBguy
post Nov 20 2017, 12:22 PM
Post#2


Access Wiki and Forums Moderator
Posts: 71,233
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Do you always get the error or only when the user just enters a whole number?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
mklein
post Nov 20 2017, 12:30 PM
Post#3



Posts: 264
Joined: 7-August 12
From: BC, Canada


From VBA help...
QUOTE
The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers)
...so it would seem that it is rounding your 0.25 to zero.

What I would do if there are only four valid options for data in a field, I would use a combobox, and force the user to make a selection in that combo.

hth
Mark

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 
BruceM
post Nov 20 2017, 12:59 PM
Post#4


UtterAccess VIP
Posts: 7,022
Joined: 24-May 10
From: Downeast Maine


I agree about the combo box (or list box) if you are choosing just the fraction of the hour, but that could be awkward if the value being tested is a number of whole hours plus a fraction (6.75 or whatever). Another option is to mutiply the textbox value by 100, and do Mod 25:

ModTime = (Me.EHW.Value * 100) Mod 25
Go to the top of the page
 
USMCdBA
post Nov 20 2017, 02:01 PM
Post#5



Posts: 158
Joined: 11-December 02
From: Charleston, SC


Thank you BruceM;
I must have misread the mod operator thingie: but doing the *100 and 25 is the answer: Works as desired. Thanks again.

KDP

--------------------
KDP
Charleston, SC -- USA
Bless those who don't need everything immediately.
Go to the top of the page
 
BruceM
post Nov 20 2017, 03:29 PM
Post#6


UtterAccess VIP
Posts: 7,022
Joined: 24-May 10
From: Downeast Maine


We are all glad to help. Best of luck with the project.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    17th December 2017 - 04:58 AM