Disregard...I was not aware that tabbing through a field, caused Excel to think there was a value. I knew a space did that. I experimented some more with the LEN function and found the solution.
I'm trying to throw some data validation in, and I'm struggling with this one for some reason. If the txt_DPDate field on a UserForm isn't entered (it does get tabbed through), I'm still getting the msgbox. What I want is, if the field is NOT empty AND is a date, to convert the format. If it is NOT empty AND is NOT a date, throw the msgbox. If it IS empty, move on, without the msgbox.
I've tried all 3 sets of code, and none seem to be working when I just tab through the field. What am I missing?
I'm trying to throw some data validation in, and I'm struggling with this one for some reason. If the txt_DPDate field on a UserForm isn't entered (it does get tabbed through), I'm still getting the msgbox. What I want is, if the field is NOT empty AND is a date, to convert the format. If it is NOT empty AND is NOT a date, throw the msgbox. If it IS empty, move on, without the msgbox.
I've tried all 3 sets of code, and none seem to be working when I just tab through the field. What am I missing?
Code:
Private Sub txt_DPDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If the DP Date value is completed with a date, the value is formatted as MM/DD/YY. If _
the DP Date is "empty", nothing happens. If the DP Date value is something other than a date, _
an error is thrown.
If Not IsEmpty(Me.txt_DPDate.Value) And IsDate(Me.txt_DPDate.Value) Then
Me.txt_DPDate.Value = Format(Me.txt_DPDate.Value, "MM/DD/YY")
ElseIf IsEmpty(Me.txt_DPDate.Value) Then Exit Sub
Else
MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
End If
'If IsEmpty(Me.txt_DPDate.Value) Then Exit Sub
'If Not IsEmpty(Me.txt_DPDate.Value) And IsDate(Me.txt_DPDate.Value) Then
' Me.txt_DPDate.Value = Format(Me.txt_DPDate.Value, "MM/DD/YY")
'Else
' MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
'End If
'If Not Len(Me.txt_DPDate.Value) = 0 And IsDate(Me.txt_DPDate.Value) Then
' Me.txt_DPDate = Format(Me.txt_DPDate, "MM/DD/YY")
'If Not IsEmpty(Me.txt_DPDate.Value) Then
' MsgBox "Please enter a valid date that the Client's Diet Plan was created, in MM/DD/YY format."
' If response = vbOK Then Me.txt_DPDate.SetFocus
'End If
'End If
End Sub
Last edited: