PDA

View Full Version : Solved: Force user to enter dates correctly?



Gingertrees
08-21-2008, 02:06 PM
I'm building an Access database with customer data, including birthdates. How do I keep users from screwing this up? The data integrity wouldn't bother me except the age function and numerous data reports hinge on this field!

For the moment they use an earlier design of mine, in the form of an Excel spreadsheet...let me just demonstrate some "creative" ways I've seen dates expressed:
12/18/097
12/18/7
12/18/19997
or, for the really "special" souls who learned to type on another planet:
12\18\97 :wot

In any case, I'd like it to freeze and give them an annoying pop-up when they try to enter it wrong. Possible? Please help. Thanks.

Mavyak
08-21-2008, 02:27 PM
While Not IsDate(strDateVariable)
MsgBox "Hey buckethead, that ain't a valid date.", vbOKOnly, "Knucklehead Alert!"
strDateVariable = InputBox("Enter a valid date:", "Need some info here...", Date())
Wend

Gingertrees
08-21-2008, 05:54 PM
LOL I like where this is going, Mavyak...
At least when I put the 12\22\89 dates in (I'm still at a loss as to how people learned this), I do get an error before the code can run. Can I make the error message say "Hey buckethead, that's not a valid date!" ? That would be hilarious.
I think this may need some ErrorHandling, but I'm not so good at that...

Private Sub Birthdate_correct()
Dim Birthdate As Date
Dim strDateVariable As String

While Not IsDate(strDateVariable)
MsgBox "Hey buckethead, that ain't a valid date.", vbOKOnly, "Knucklehead Alert!"
strDateVariable = InputBox("Enter a valid date:", "Need some info here...", Date)
End Sub

Mavyak
08-21-2008, 06:23 PM
Is the birth date field in the table a datetime data type? If so, I don't think Access will accept an invalid date in the field. If you are capturing data in a stand-alone form (without a bound table/query) and passing the data to the correct tables via VBA, then I would think the value would be check in the OnExit event of the applicable control on the form.

Gingertrees
08-22-2008, 05:34 AM
Yeah, my mistake. Access is naturally more precise that Excel. As I said, I'm working based on the Excel spreadsheet currently in use, which I did not have the presence of mind to pre-program to limit that.

I still wish i could make the error message say "Hey buckethead..."
Thanks. :-)

CreganTur
08-22-2008, 05:52 AM
Another thing to consider:

Setting up an input mask for your textboxes will show users how data should be entered, as well as enforcing data entry.