validate date, using calendar

J

Josh

I have the following code for a textbox, using to validate a date:

If Me.txtPOdate > Date + 60 Then
Select Case MsgBox("Date is greater than 60 Days from today. Have you perhaps
entered an incorrect year or month? If the date is correct, click OK. If you
need to correct the date, click Cancel.", vbOKCancel Or vbQuestion Or
vbDefaultButton1, "Possible Incorrect Date Warning")
Case vbOK
'just continue on
Case vbCancel
Cancel = True
Me.txtPOdate.SelStart = 0
Me.txtPOdate.SelLength = Len(Me.txtPOdate.Text)
Exit Sub
End Select
End If

which seems to work fine. This is if user wants to type in the date. I also have
a calendar, Allen Browne's http://allenbrowne.com/ser-51.html
(I know that by using the calendar picker you mostly avoid the need for this
type of validation, but just trying to cover all my bases)

I'm trying to have the same sort of validation if user uses the calendar picker,
but I can't use the beforeupdate of txtPODate. I assume because the calendar
picker is inserting the date.

How can I have the calendar picker itself do the validating? Or some other
suggestion?

Thanks, Josh (and thanks to Allen Browne, the calendar picker works great)
 
A

Allen Browne

Josh, what I do is to put all those validation checks into the BeforeUpdate
event of the form, rather than the specific controls. This means the user
gets just one dialog to deal with for any warnings immediately before the
record saves, rather than annoying them with constant dialogs as they edit
the record.

This kind of thing:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim bWarn as Boolean
Dim strMsg As String

'Handle the critical items first
If IsNull(Me.Field1) Then
Cancel = True
strMsg = strMsg & "You must enter Field1." & vbCrLf
End If
If Me.EndDate < Me.StartDate Then
Cancel = True
strMsg = strMsg & "End date must be after Start date." & vbCrLf
End If

'Now any warnings if there were no critical items.
If Not Cancel Then
If Me.txtPOdate > Date + 60 Then
bWarn = True
strMsg = strMsg & "PODate is future." & vbCrLf
End If
If IsNull(Me.Surname) Then
bWarn = True
strMsg = strMsg & "Surname is blank." & vbCrLf
End If
End If

'Show the response
If Cancel Then
strMsg = strMsg & vbCrLf & "Fix the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, "Invalid entry"
ElseIf bWarn Then
strMsg = strMsg & vbCrLf & "Proceed anyway?"
If MsgBox(strMsg, vbYesNo+vbDefaultButton2, "Warning") <> vbYes Then
Cancel = True
End If
End If
End Sub
 
J

Josh

Been out of town, just saw this message.

Thanks, your suggestion makes sense.....to me.

For what it's worth, with another DB, I did this sort of validation,
(beforeUpdate of Form) on some required fields. User's complained about NOT
getting a warning on each failed validation....can't win for losing. <g>

However, in this case, I think I will go with your suggestion.

Thanks again............Josh
 

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