Date validation

L

LaoYao

Could any of you help me with this?

I have a textbox that is for a date entry. The format is MM/YYYY. Now,
I want to validate this field so it cannot be prior to 01/1982 or
greater than current date.

Any help would be greatly appreciated.

Thanks!
 
D

Douglas J. Steele

Untested air-code:


Function ValidDateValue(MMYYYYString) As Boolean

Dim lngMonth As Long
Dim lngYear As Long

ValidDateValue = False

If Len(MMYYYYString) = 7 And _
InStr(MMYYYYString, "/") = 3 Then

lngMonth = CLng(Left(MMYYYYString, 2))
lngYear = CLng(Mid(MMYYYYString, 4))

If lngMonth >= 1 And lngMonth <= 12 Then
If lngYear >= 1982 And lngYear <= Year(Date()) Then
If lngYear = Year(Date()) Then
If lngMonth <= Month(Date()) Then
ValidDateValue = True
End If
Else
ValidDateValue = True
End If
End If
End If
End If

Exit Function
 
J

Joseph Meehan

LaoYao said:
Could any of you help me with this?

I have a textbox that is for a date entry. The format is MM/YYYY. Now,
I want to validate this field so it cannot be prior to 01/1982 or
greater than current date.

Any help would be greatly appreciated.

Thanks!

I would suggest changing it to a date-time type. However that has its
own potential problmes. Text is not suitable for easy calculations.
 
L

LaoYao

Doug,

You codes is what exactly I need, but how can I associate it with the
date control?
 
D

Douglas J. Steele

Not sure exactly what you mean by associating it with the date control.

If the text box is bound to a field in the form's underlying recordset, you
can use that function in the text box's BeforeUpdate event.

Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)

If Not ValidDateValue(Me.MyTextBox) Then
MsgBox "Invalid Date"
Cancel = True
End If

End Sub
 

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