Problems with Dates

G

Guest

Why won't this work right. I have a data entry screen where the user enters
a date. The date range that should be entered is 10/1/2002 to 9/30/2005. I
have the following code in as:

Dim TextDate As Date
TextDate = DateSerial(Right(Me!INDPTDOS.Value, 4), _
Left(Me!INDPTDOS.Value, 2), _
Mid(Me!INDPTDOS.Value, 3, 2))
Select Case TextDate
Case Is < #10/1/2002#
' do nothing
Case Is > #9/30/2005#
' do nothing
Case Else
MsgBox "You have entered an invalid date."
Cancel = True
End Select

This will still enter dates such as 9/25/2000 and any date over 10/1/2005
which I DO NOT WANT TO HAPPENed. How can I get it so that the user will not
enter any dates except those in this range 10/1/2002 to 9/30/2005?
 
S

Steven Britton via AccessMonster.com

Not an expert here but, is using the dateserial as the textdate then using
date perameters #10/1/2002# causing the confusion? You may want to find
the dateserial of #10/1/2002# and use that.

Or try TextDate = Me!INDPTDOS and see what results.

This is something that I use that is similar to check dates, but doesn't
check for above or below.

'Checks for a Beginning Date
If IsNull(Me.txtStartDate) = True Or Me!txtStartDate = "" Then
MsgBox "You must enter a Beginning Date", , "Date Error"
Me.txtStartDate.SetFocus
Exit Sub
End If
' Checks for an Ending Date
If IsNull(Me.txtEndDate) = True Or Me!txtEndDate = "" Then
MsgBox "You must enter a Ending Date", , "Date Error"
Me.txtEndDate.SetFocus
Exit Sub
End If
' Checks to see that the Ending Date isn't before the Beginning Date
If Me.txtStartDate > Me.txtEndDate Then
MsgBox "Ending Date cannot be before Beginning Date", vbOKOnly, "Date
Error"
Me.txtEndDate.SetFocus
Exit Sub
 
G

George Nicholson

As written, you are doing nothing if the date is less than 10/1/02, doing
nothing if the date is greater than 9/30/05 and displaying an error message
for any date that falls between. You might try:

Select Case TextDate
Case #10/1/2002# to #9/30/2005#
'Do nothing
Case Else
MsgBox "You have entered an invalid date."
Cancel = True
End Select

OR

Select Case TextDate
Case Is < #10/1/2002#
MsgBox "You have entered an invalid date."
Cancel = True
Case Is > #9/30/2005#
MsgBox "You have entered an invalid date."
Cancel = True
Case Else
'Do nothing
End Select

HTH,
 

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

Similar Threads


Top