John Vinson said:
Two ways I can think of to do that. By default, if you enter a date
with a single slash (3/5) which can be correctly interpreted as a
month and a day, Access will fill in the current year. IF you will
NEVER be entering birthdates for babies under a year old, you can take
advantage of this fact in the textbox's AfterUpdate event:
Private Sub txtBirthdate_AfterUpdate()
If Year(Me.txtBirthdate) = Year(Date) Then
Me.txtBirthdate = DateSerial(104, _
Month(Me.txtBirthdate), Day(Me.txtBirthdate))
End If
End Sub
The alternative would be to use an unbound textbox with code to check
for the number of slashes... more code than I want to write off the
top of my head this afternoon!
Use the Split function. Assuming the unbound textbox is named MyTextField,
and there's a bound textbox txtBirthday (which needn't be visible...):
Private Sub MyTextField_AfterUpdate()
Dim varDates As Variant
varDates = Split(Me.MyTextField, "/")
' varDates will now be an array containing the values
' in MyTextField without the slashes.
' For instance, 3/5 will result in varDates(0) being equal
' to 3, and varDates(1) being equal to 5, while
' 3/5/1967 will result in varDates(0) being equal to
' 3, varDates(1) being equal to 5, and varDates(2)
' being equal to 1967.
Select Case UBound(varDates)
Case 0
' No slashes in what was input
MsgBox "You didn't enter a valid date."
Case 1
' 1 slashes: assume only month and day
Me.txtBirthday = DateSerial(104, varDates(0), varDates(1))
Case 2
' 2 slashes: assume month, day, year
Me.txtBirthday = DateSerial(varDates(2), varDates(0), varDates(1))
Case Else
' Someone needs to learn how to type...
MsgBox "You didn't enter a valid date."
End Select
End Sub
Note that this assumes Access 2000 or newer.