One way to see how the displayed text is interpreted by VFP is to have 2 textboxes on a form: Text1 and Text2.
In the Text1 textbox Keypress and InteractiveChange events program this line of code:
Code:
Thisform.Text2.Value = This.Value
(You gotta keep the LPARAMETERS in keypress, otherwise any keypress will error, but you only add this one line to beoth events.)
Initialize Text1 and Text2 with the empty date by setting both their values to CTOD(''), for example in Form Init you could do
STORE CTOD('') TO Thisform.Text1.Value, Thisform.Text2.Value
Now using text1 you still will have the "Invalid Date", that code in Keypress doesn't handle that, but cope with that for a while, there are some stages to observe here:
First, remember I put the value of text1 into text2, that way the text2 box just makes visible what the value of text1 is. And you can see an empty date there, as long as the typed in text portion like "11/ / " is not yet a complete date. You also see that VFP autocompletes whenever it can, it does interpret a date for the current year, when no year digit is entered yet, it will expand a year with century, as long as only 1 or 2 digits of the year are entered. You can also try out a number like 89, which will be expanded to 1989. There's a roll over number you can also define with the ROLLOVER clause of SET CENTURY - see the help on that for the details.
Now the good news, unlike I thought the entry of month 13 (as in the 7th stage) or 29th of a February in a non leap year (9th stage) does not in itself trigger the "Invalid Date" messge you can't override, the moment that happens is when you want to leave the textbox while the value is still empty. And as already said, you can check EMPTY(Text.Value) when it's a date type value, which is the case here when initialized with a date, any date, also the empty date. The only case where VFP will allow leaving and not alert with "Invalid Date" is the user actually entered nothing or only spaces and the text1.text is still just the empty date. You could ignore that case, though, if you want a user that activated such a textbox to only get out with a valid date, you can still make that a check of the Text1.Valid event.
Anyway, the focus of this good news is, that you actually now only need to check Keypress for keys that trigger leaving the textbox. If there is a TAB, BBACKTAB, ENTER while the Text1.Value is the empty date, you could stop that from happening by NODEFAULT in the Keypress event. Which means the user will not leave the Textbox and no "Invalid Date" messge appears. And then it's your turn to either display another message, indicate the invalid date with a backcolor change or whatever else.
The bad news is illustrated in the last 9th stage: The text cursor here is before the 3 of 2003, the date is not valid in that year, therefore text1.value (and text2 display) is the empty date, now not only TAB, BACKTAB, and ENTER leave the control at an invalid state, also a RIGHT ARROW key would. You could not always react to a RIGHT ARROW key with your own way of stating the invalid date, though, the user should be allowed to move through the digits with the text cursor to change them and make it a valid date.
So that's now the complicated corner case. Unfortunately there are more: Whenever the user leaves the date textbox with a mouse click, not only to another control but also to another window or even another application. You can't trap that in Keypress, now you'd need to act in all kind of event of any other control and form, which would break encapsulation to do that. So that'll be the Achilles heel, but you can easily trap the case of TAB/BACKTAB/ENTER leaving the textbox when it's text has more than just the spaces and date mark characters and the textbox value is the empty date. That can be prevented and signaled in another way.
PS: There's a good news within that bad news, that even without trying to catch the spilled milk of cases where mouse clicks lead to the "Invalid Date" message, that's not breaking the world, the unfortunate "Invalid Date" message will appear, but that also prevents leaving the textbox in an invalid date state. It's not very likely to happen, if a user is about entering a date, why would he pick up the mouse before he finshes that?
Chriss