How to show an error when time out is less than time in.

J

Joy

I have a form in which we enter the time in (set as Medium Time) and time out
(set as Medium Time) and then another box I have a formula (=[Time Out]-[Time
In]) which shows the time spent. Is there a way to have an error window pop
up if the time out is before the time in? Or someway to stop an entry being
made?
 
K

Ken Snell \(MVP\)

Use the form's BeforeUpdate event to test the conditions:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.[Time Out].Value & "") = 0 Then
MsgBox "You must enter a value for the Time Out data!"
Cancel = True
ElseIf Len(Me.[Time In].Value & "") = 0 Then
MsgBox "You must enter a value for the Time In data!"
Cancel = True
ElseIf DateDiff("s", Me.[Time Out].Value, Me.[Time In].Value) > 0 Then
MsgBox "Time Out value is earlier than Time In value!"
Cancel = True
End If
End Sub
 
J

John W. Vinson

I have a form in which we enter the time in (set as Medium Time) and time out
(set as Medium Time) and then another box I have a formula (=[Time Out]-[Time
In]) which shows the time spent. Is there a way to have an error window pop
up if the time out is before the time in? Or someway to stop an entry being
made?

Does your field contain just a time, or a date and time? Will you ever have
the situation where the TimeIn is 11:50pm and the TimeOut 2:30am? If so you
may want to consider using both the date and time, not just the time.

That said... in your Form's BeforeUpdate event you can click the ... icon,
choose Code Builder, and edit the code to:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If [Time In] > [Time Out] Then
MsgBox "You can't finish before you start!!!"
Cancel = True
Me![Time Out].SetFocus
End If
End Sub


John W. Vinson [MVP]
 
J

Joy

I must be doing something wrong because I can't get either of these to work.
I only use time. Date is not an issue. Can you offer some more advice?
--
Thanks, Joy


John W. Vinson said:
I have a form in which we enter the time in (set as Medium Time) and time out
(set as Medium Time) and then another box I have a formula (=[Time Out]-[Time
In]) which shows the time spent. Is there a way to have an error window pop
up if the time out is before the time in? Or someway to stop an entry being
made?

Does your field contain just a time, or a date and time? Will you ever have
the situation where the TimeIn is 11:50pm and the TimeOut 2:30am? If so you
may want to consider using both the date and time, not just the time.

That said... in your Form's BeforeUpdate event you can click the ... icon,
choose Code Builder, and edit the code to:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If [Time In] > [Time Out] Then
MsgBox "You can't finish before you start!!!"
Cancel = True
Me![Time Out].SetFocus
End If
End Sub


John W. Vinson [MVP]
 
K

Ken Snell \(MVP\)

Tell us how you set up the approaches/code, and what happens when you "run"
the code. Also, show us some examples of data that, when entered, should
produce the error that you want to bring to the user's attention.
--

Ken Snell
<MS ACCESS MVP>


Joy said:
I must be doing something wrong because I can't get either of these to
work.
I only use time. Date is not an issue. Can you offer some more advice?
--
Thanks, Joy


John W. Vinson said:
I have a form in which we enter the time in (set as Medium Time) and
time out
(set as Medium Time) and then another box I have a formula (=[Time
Out]-[Time
In]) which shows the time spent. Is there a way to have an error window
pop
up if the time out is before the time in? Or someway to stop an entry
being
made?

Does your field contain just a time, or a date and time? Will you ever
have
the situation where the TimeIn is 11:50pm and the TimeOut 2:30am? If so
you
may want to consider using both the date and time, not just the time.

That said... in your Form's BeforeUpdate event you can click the ...
icon,
choose Code Builder, and edit the code to:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If [Time In] > [Time Out] Then
MsgBox "You can't finish before you start!!!"
Cancel = True
Me![Time Out].SetFocus
End If
End Sub


John W. Vinson [MVP]
 

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