View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default TextBox validation code

Try the following. Let me know if not working as you want it to but let me
know what locale date format you are using.

Private Sub TextBox2_AfterUpdate()

Dim bolValid As Boolean
Dim dateValid As Date

On Error Resume Next
dateValid = DateValue(Me.TextBox2)
On Error GoTo 0

If dateValid 0 Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date format in TextBox2" _
& vbCrLf & "Must be d/m/y format"
Exit Sub
End If

If Year(dateValid) = Year(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Year in TextBox2" & _
vbCrLf & "Must be " & Year(Date)
Exit Sub
End If

If Month(dateValid) = Month(Date) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid Month in TextBox2" & _
vbCrLf & "Must be " & Month(Date)
Exit Sub
End If

If dateValid = DateValue(Me.TextBox1) Then
bolValid = True
Else
bolValid = False
MsgBox "Invalid date in TextBox2" & _
vbCrLf & "Must be = to TextBox1"
Exit Sub
Exit Sub
End If

'You should not need following code
'Only there for testing
If bolValid Then
MsgBox "Date in TextBox2 is valid"
End If

End Sub

--
Regards,

OssieMac