Input mask for decimal quarters

G

Guest

I am trying to create an input mask for a timesheet program that only allows
increments of a quarter of an hour. XX.00, XX.25, XX.50 or XX.75. For the
timesheet I need to cater for everything from 0.25 hrs to 24 hrs. I thought
of a combo box but it is a bit clumsy. Can anyone help with and input mask
that will work.
 
G

Guest

I would not use a combo box for this. It would be clumsy. Unfortunately, I
don't believe an InputMask would do it for you. You are requesting specific
digits, and I don't think an InputMask can do that.

I think what you really need is a validation routine. One other thing, if
you are using a date/time data type to store the results, your XX.nn will
not work correctly.
..25 would be seen as 25 minutes, .5o would be seen as 50 mimutes, and .75
would cause an error. With a little clear conversion code, you could display
it that way, but you will need to store it as XX.00, XX.15, XX.30, XX.45
 
P

Philo Hamel via AccessMonster.com

I don't know if this is really what you're looking for and there probably
is a better way of coding it, but I had such a fun time trying to find a
solution that I had to write it down. :)

Here goes!

I think you're better of with a field in your table that isn't a Date/Time
one, since you want to store the AMOUNT of hours/minutes and not the time.
So I suggest using the 'Number' DataType for this one.
Go to the table and make a Field with these properties:
- FieldSize: Single
- DecimalPlaces: 2
- ValidationRule: Between 0 And 23.99

Now open your form in desing mode. Select the textfield that will be used
to type the amount of hours. Make sure it doesn't use a predefined format.
Then add a AfterUpdate event like this one:

Private Sub [your textfield name]_AfterUpdate()
Dim number, number2
number = Int(Me.[your textfield name])
number2 = Me.[your textfield name]- number
Select Case number2
Case 0 To 0.125
Me.[your textfield name] = Me.[your textfield name] - number2
Case 0.126 To 0.365
Me.[your textfield name] = Me.[your textfield name] - (number2 - 0.25)
Case 0.366 To 0.625
Me.[your textfield name] = Me.[your textfield name] - (number2 - 0.5)
Case 0.626 To 0.885
Me.[your textfield name] = Me.[your textfield name] - (number2 - 0.75)
Case 0.896 To 1
Me.[your textfield name] = Me.[your textfield name] - (number2 - 1)
End Select
End Sub

Don't forget to replace the [your textfield name] when copying the code ;)

Hope this works for you and again: this is probably a quite rudimentary
solution, but it's as far as my VB skills go.

Later,
Philo
 
P

Philo Hamel via AccessMonster.com

Oops! Small but important error. 'Case 0.896 To 1' has to be 'Case 0.886 To
1'. Sorry about that.

Philo
 
R

RuralGuy

NevilleT:
I am trying to create an input mask for a timesheet program that only
allows increments of a quarter of an hour. XX.00, XX.25, XX.50 or
XX.75. For the timesheet I need to cater for everything from 0.25 hrs
to 24 hrs. I thought of a combo box but it is a bit clumsy. Can
anyone help with and input mask that will work.

I used a txtBox with the following code:

Private Sub txtHours_BeforeUpdate(Cancel As Integer)
Dim MyArray As Variant
'-- Test for clearing this TextBox

If Not IsNumeric(Me.txtHours) Then
Me.Undo
Cancel = True
Else
If Me.txtHours <> Int(Me.txtHours) Then
'-- There is a fractional component. Make sure it is divisible
by .25
MyArray = Split(CStr((Me.txtHours)), ".")
If Len(MyArray(1)) = 1 Then
MyArray(1) = MyArray(1) & "0" '-- Pad out to 2 places
ElseIf Len(MyArray(1)) > 2 Then
MyArray(1) = Left(MyArray(1), 2) '-- Truncate to 2 places
End If
If Val(MyArray(1)) Mod 25 Then
MsgBox "Entry must be in 1/4 hour increments!"
Cancel = True
End If
End If
End If

End Sub

HTH

RuralGuy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top