Tandy said:
Dirk Goldgar,
Hi! Thank you for the help, it is exactly what I wanted. I
would also like to know what you change to have it enter a short date
(1/1/2006) and a month and year date (1/2006). Thank you so much for
your time and effort. I really appreciate it!
Assuming that the field that will hold these default values is a
date/time field, the *simple* code for both would be something like
this:
'----- start of code -----
Private Sub Form_Open(Cancel As Integer)
Dim strDefault As String
Dim fGotDefault As Boolean
Do
strDefault = InputBox("Enter the default date:")
If Len(strDefault) = 0 Then
fGotDefault = True
Else
If IsDate(strDefault) Then
fGotDefault = True
Else
MsgBox "That's not a valid date!"
End If
End If
Loop Until fGotDefault
If Len(strDefault) > 0 Then
Me!YourDateField.DefaultValue = "#" & strDefault & "#"
End If
End Sub
'----- end of code -----
That's using "YourDateField" as the name of the field.
As I said, this doesn't make any distinction between a "short date" and
a "month and year" date. That's because there *is* not such thing as a
"month and year" date, internally -- at least, not if you store the
value in a date/time field. The date data type always includes month,
day, and year, even if the day isn't displayed due to an input mask or
format property.
In the code above, I took the easy way of validating the user's entry:
I just said that it had to be interpretable as a date. That's what the
IsDate() function does; it tells whether the argument that was passed
to it can be understood as a date. Using this as the edit, both
"1/1/2006" and "1/2006" can both be entered, and both will be accepted
and understood as the same date, as would "Jan 1, 2006". Beware,
though: depending on the regional date settings, "1/5/2006" may be
interpreted as January 5th on one system, and as May 1st on another.
Also, if the user enters "1/06" instead of "1/2006", Access will
interpret that as January 6th of the current year, not as January 1st of
2006.
If you wanted to pin down the user's default date entry with more
precise editing, to eliminate any ambiguity and require some particular
format, that would be possible. It wold take more complicated code,
though.