Date and Time Condition

G

Guest

Hi all,

I have a small problem with user enter the wrong date or time. Here is my
problem on an input form: The user sometimes enters the wrong time or date
Example
(Text Boxes on the form)
Date1 Time1 Date2 Time2
01/01/2005 23:00 01/01/2005 23:44 This is correct
01/02/2005 19:25 01/01/2005 21:55 This is correct
01/01/2005 20:22 01/01/2005 20:11 This is WRONG

The date should be 01/02/2005!! How can I control this so the user can't
enter the wrong date or time. I need some type of code for Date2 and Time2.
How can I code this? Thanks
 
G

Guest

Since one can't tell the user that "Date2 is wrong!" you should find
something more descriptive. Here's an example where the check in date and
time must be later than the checkout date and time. BTW, one can use a
Date/Time field to store both the date and the time, but if they need to be
separated for display purposes, then the DateValue( ) and TimeValue( )
functions can be used.

Private Sub ChkInDate_BeforeUpdate(Cancel As Integer)

On Error GoTo ErrHandler

If (Me!ChkInDate.Value < Me!ChkOutDate.Value) Then
MsgBox "You cannot check in the item" & vbCrLf & _
"before you check it out!", vbCritical + vbOKOnly, "Invalid Date!"
Cancel = True
End If

Exit Sub

ErrHandler:

MsgBox "Error in ChkInDate_BeforeUpdate( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub


Private Sub ChkInTime_BeforeUpdate(Cancel As Integer)

On Error GoTo ErrHandler

If (Me!ChkInTime.Value < Me!ChkOutTime.Value) Then
If (Me!ChkInDate.Value <= Me!ChkOutDate.Value) Then
MsgBox "You cannot check in the item" & vbCrLf & _
"before you check it out!", vbCritical + vbOKOnly, _
"Invalid Date And Time!"
Cancel = True
End If
End If

Exit Sub

ErrHandler:

MsgBox "Error in ChkInTime_BeforeUpdate( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub


HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
G

Guest

You're very smart, thank you.

'69 Camaro said:
Since one can't tell the user that "Date2 is wrong!" you should find
something more descriptive. Here's an example where the check in date and
time must be later than the checkout date and time. BTW, one can use a
Date/Time field to store both the date and the time, but if they need to be
separated for display purposes, then the DateValue( ) and TimeValue( )
functions can be used.

Private Sub ChkInDate_BeforeUpdate(Cancel As Integer)

On Error GoTo ErrHandler

If (Me!ChkInDate.Value < Me!ChkOutDate.Value) Then
MsgBox "You cannot check in the item" & vbCrLf & _
"before you check it out!", vbCritical + vbOKOnly, "Invalid Date!"
Cancel = True
End If

Exit Sub

ErrHandler:

MsgBox "Error in ChkInDate_BeforeUpdate( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub


Private Sub ChkInTime_BeforeUpdate(Cancel As Integer)

On Error GoTo ErrHandler

If (Me!ChkInTime.Value < Me!ChkOutTime.Value) Then
If (Me!ChkInDate.Value <= Me!ChkOutDate.Value) Then
MsgBox "You cannot check in the item" & vbCrLf & _
"before you check it out!", vbCritical + vbOKOnly, _
"Invalid Date And Time!"
Cancel = True
End If
End If

Exit Sub

ErrHandler:

MsgBox "Error in ChkInTime_BeforeUpdate( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub


HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 

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