textbox - format for dates (mm/dd/yyy)

  • Thread starter Thread starter barrfly
  • Start date Start date
B

barrfly

I have a text box in a userform. The box will be used to manually ente
a date, so I would like the "/" to appear between the month & day
year as the date is entered as well as not allow invalid date entries.


B
 
Hi BA,

There are several ways to do this. The best may be to use a specialized
control instead of a TextBox...perhaps the Date/Time Picker. But you'll
have to check licensing requirements and availability on client machines
before using one.

If you want to use a textbox, you could put some code behind it like this:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
If IsDate(.Text) Then
.Text = Format$(.Text, "Short Date")
Else
.SelStart = 0
.SelLength = Len(.Text)
MsgBox "Invalid date entered.", vbExclamation
Cancel = True
End If
End With
End Sub


If you want the /'s to show up, then it would be somewhat trickier. Your
best bet in that case may be to use 3 separate textboxes separated by labels
with captions of "/". Then you could validate the concatenation of the
textboxes assuming all three are filled in. If all three aren't filled in,
you can raise an error when they attempt to proceed.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Back
Top