Date Input (1 Viewer)

kitty77

Registered User.
Local time
Today, 10:09
Joined
May 27, 2019
Messages
712
I have a date field with a date picker on a form. How can I prevent the user from inputting the wrong year? Basically if the data entry is this year then use 2020.
I want to prevent an incorrect year.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:09
Joined
Oct 29, 2018
Messages
21,467
The best place to validate data entry is in the BeforeUpdate event. In this case, you might try using the Control's BeforeUpdate event.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:09
Joined
Feb 28, 2001
Messages
27,171
The date field with a date picker on a form has events such as control_CHANGE and control_LOSTFOCUS. In either of those events, you could perform a simple check as to whether a date was picked and, if so, was valid. You can look at the DatePart function which can give you a four-digit year as one of its returns.


The above is the quick&dirty reference for same.

Basically, if you have a date in the control in the CHANGE or LOSTFOCUS event then you could compare to the year for the current date.

Code:
IF ( Datepart("yyyy", control-name ) <> Datepart("yyyy", Date() ) ) AND ( NZ(control-name),"") <> "" ) THEN
    complain and do something about it
END IF

I didn't write more than that because you didn't suggest what you really wanted to do and you also have choices about when you would use this. But this is the basics of it.
 

Cronk

Registered User.
Local time
Tomorrow, 00:09
Joined
Jul 4, 2013
Messages
2,772
Be aware of situations at the beginning and end of a year. For example, a situation where data is being entered on January 2 for the previous year.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:09
Joined
Feb 19, 2002
Messages
43,257
The only correct events for validation are events where you can cancel saving the record. If you do your validation in the LostFocus event, the user can simply blow by your error and the bad data will be saved. If you do your validation in the Change event, same problem except worse since the Change event runs for each character entered in the control. so if the user types in a date, the event is going to run 10 times and only the last time will the data be valid For a Date Picker, it probably runs only once but I don't know and I never would find out since I would never use the Change event for this validation.

If you want the user to know IMMEDIATELY before proceeding to the next control that he has made an error, then you need to put your validation code in the control's BeforeUpdate event. Keep in mind though you can't use the Control's BeforeUpdate event for validation if part of the validation is a check for presence. If a control never gets the focus, its events don't run. Therefore, there is no control level event that can ensure that a control has some data in it. The best place to put your validation where it is in one and only one place is the form's BeforeUpdate event. Think of this event as the flapper on a funnel. In order to save, the record has to pass through this funnel. You do the validation and if the record is invalid, you simply lock the flapper shut with:
Cancel = True
And that will prevent Access from saving the invalid data.

The only time I put validation code into the control's BeforeUpdate event is in the case where I am going to prevent the user from saving the record if the field in question is invalid. So, if you need an SS to save the record, then why let the user enter dozens of fields and get to the end only to find out that he can't save, period? In that case and only that case, do I put validation anywhere but the form's BeforeUpdate event.
 

Users who are viewing this thread

Top Bottom