none
Access Date Control - how to test date RRS feed

  • Question

  • I have a control (Text Box) on a Bound form to enter the date.

    After the date is entered I want to test if the date is within a limited range, display an error message if not and then refocus on the date field so the user can't move to the next control without entering a valid date. I have tried testing the date after Control events like Lost Focus and Exit and also after the next control is clicked.

    If the user does not press enter after typing the date or uses the date picker without pressing enter then it appears that the date control value is not updated so the test is looking at the last entered date.

    Sooo, two questions:

    How do I make sure that I am testing the date entered without requiring the user to press Enter?

    If the date entered fails the test I want to return focus to the Date Control and wait for the user to enter a new date. I have tried Me!DateField.SetFocus and Screen.PreviousControl.SetFocus and neither seems to work. Processing continues with the code after the date test logic.

    Thursday, January 12, 2017 4:00 PM

Answers

  • My problem is that the control value is not set until Enter is pressed, e.g. if I type 1/11/17 in the date control and then test Me!NewDate after the control loses focus or BeforeUpdate the value is null or the last date entered so I am testing the previously entered date.

    It does not matter whether I type the date or use the Date Picker, the control value is not set unless Enter is pressed so any test will be looking at the last entered date.

    I did not see a Control event or parameter that corresponded to Enter but Screen.PreviousControl.SetFocus seems to work. It does not appear to actually move the focus but it does set the control value to what was typed in.

    Saturday, January 14, 2017 12:52 AM

All replies

  • Hi,

    I noticed you are looking for some codes for Date Control focus and enter feature.

    Please understanding that this forum focuses on general discussion for Office applications. To get more suggestion on codes, I would move the thread to Access for developers forum for more help. Hope you can find the solution there :)


    Regards,

    Winnie Liang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, January 13, 2017 6:55 AM
  • After the date is entered I want to test if the date is within a limited range, display an error message if not and then refocus on the date field so the user can't move to the next control without entering a valid date. I have tried testing the date after Control events like Lost Focus and Exit and also after the next control is clicked.

    Hi BVitter,

    Did you try  the BeforeUpdate event of the control? If the entered value is not a valid one, the cancel parameter is set to True.

    Imb.

    Friday, January 13, 2017 9:02 AM
  • My problem is that the control value is not set until Enter is pressed, e.g. if I type 1/11/17 in the date control and then test Me!NewDate after the control loses focus or BeforeUpdate the value is null or the last date entered so I am testing the previously entered date.

    It does not matter whether I type the date or use the Date Picker, the control value is not set unless Enter is pressed so any test will be looking at the last entered date.

    I did not see a Control event or parameter that corresponded to Enter but Screen.PreviousControl.SetFocus seems to work. It does not appear to actually move the focus but it does set the control value to what was typed in.

    Saturday, January 14, 2017 12:52 AM
  • Hi,

    Pardon me but are you saying you already found a fix or are you still looking for some help with validating the date entered?

    Just curious..

    Saturday, January 14, 2017 1:26 AM
  • My problem is that the control value is not set until Enter is pressed, e.g. if I type 1/11/17 in the date control and then test Me!NewDate after the control loses focus or BeforeUpdate the value is null or the last date entered so I am testing the previously entered date.

    Hi BVitter,

    What are you testing? The value in the control or the value in the record? The value in the record is only set after saving the record, e.g. by moving to a different record.

    Imb.

    Saturday, January 14, 2017 7:23 AM
  • I found what I think is a fix, still not sure why it works or if it will always work
    Saturday, January 14, 2017 2:02 PM
  • I need to test the "value in the control" to make sure it is within the allowable range before the record is saved.

    My challenge is that the value I test "Me!NewDate" does not reflect the date just entered unless I press Enter after entering the date - either typed in or by the Date Picker.

    The date test occurs in the On Click Event for a Control I press after entering the date.

    Saturday, January 14, 2017 2:09 PM
  • I found what I think is a fix, still not sure why it works or if it will always work

    It may not always work if the user clicks all over the form first before entering a date. Or, if they don't click anywhere at all.

    A better approach might be is to use the date control's Change event but you'll have to test its Text property rather than the Value. Also, keep in mind the Change event will fire for every kestroke/character they enter into the textbox.

    Hope it helps...

    Saturday, January 14, 2017 4:13 PM
  • I need to test the "value in the control" to make sure it is within the allowable range before the record is saved.

    My challenge is that the value I test "Me!NewDate" does not reflect the date just entered unless I press Enter after entering the date - either typed in or by the Date Picker.

    Hi BVitter,

    If the value is entered manually, you can use the BeforeUpdate event to check the value. In case of an invalid value, you can set the cancel parameter to true.

    If the value is entered by some code, the BeforeUpdate event is not fired, but then you can use the Exit event. This event has also a cancel parameter that can be set to true in case of an invalid value.

    Imb.

    Saturday, January 14, 2017 4:18 PM