Incorrect Date input not flagged

User avatar
silverback
5StarLounger
Posts: 774
Joined: 29 Jan 2010, 13:30

Incorrect Date input not flagged

Post by silverback »

A form has an unbound TextBox field which has a Format property of Short Date. A user entered a value of 31/4/5 (i.e. incorrect - there is no 31st April) but Access accepted this input, with the result that value stored was 5/4/1931

How can I stop this behaviour so that 31/4/5 is errored as incorrect for that field, please?

Thanks
Silverback

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Incorrect Date input not flagged

Post by HansV »

Date input is processed by Windows - it tries to interpret the entry as best it can according to the user's system settings. Since 31 April is not a valid date, Windows assumes that the user intended 31 to be the year. You can't really prevent that.

Some possible ways to get "correct" dates:
  • Use the Before Update event of the text box to check for "unlikely" dates - for example, if the date is supposed to be in this century, check whether the year is between 2000 and the current year.
  • Use the Calendar control.
  • Use separate text boxes for day, month and year, and perform your own validity checks.
Best wishes,
Hans

User avatar
silverback
5StarLounger
Posts: 774
Joined: 29 Jan 2010, 13:30

Re: Incorrect Date input not flagged

Post by silverback »

Hans
Didn't expect that answer, but now I know, can sort our some sensible validation.
Thanks
Silverback