Solved

Calendar Controls

Posted on 2016-08-25
5
65 Views
Last Modified: 2016-08-26
I am using a calendar control so the users can pick their date.  However, some of them like to type in this field instead and are not entering the information correctly, example:  05/03/201.  This will still be accepted.  Is there a way to keep using the calendar control but make sure the dates entered are valid dates?  Or to keep them from typing in this field and force them to use the control?
0
Comment
Question by:Annmv888
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 38

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41770882
The easiest way to keep people from making mistakes is to minimize what they have to type.  When entering a date, they never have to enter the year if it is the current year and they only have to enter two digits at that.  So - 8/25 tab would allow Access to complete the entry as 8/25/2016.

To prevent out of range dates, use the control's BeforeUpdate event.

If Me.txtStartDate < Date() - 180 or Me.txtStartDate > Date() + 180 Then
    If Msgbox("Start date is out of range. Is this correct?", vbYesNo) = vbNo Then
        Cancel = True
        Me.txtStartDate.Undo
        Exit Sub
    End If
End If

The "Cancel = True" expression cancels the update and prevents the value from being saved.  The Me.txtStartDate.Undo removes what the user typed and reverts the control to its contents before the user started typing.

If you need to make dates required, the best option is to set the required flag on the table itself.  If they are optionally required, then you need to put the code into the FORM's BeforeUpdate event.  You would also use the FORM's BeforeUpdate event if you needed to compare the contents of two dates.  Here's some examples of validation code.

If IsNull(Me.txtSaleDate)  Then
    Cancel = True
    Msgbox "Sale Date is required.",vbOKOnly
    Me.txtSaleDate.SetFocus
    Exit Sub
End If

If Me.txtStartDate > Me.txtEndDate Then
    Cancel = True
    Msgbox "Start Date must be <= End Date", vbOKOnly
    Me.txtStartDate.SetFocus
     Exit Sub
End If
0
 
LVL 4

Expert Comment

by:bfuchs
ID: 41770906
why not use controls input mask property, set it to something like "00/00/0000;0;_"
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 41770994
If you use an input mask,
1. The Access calendar control is disabled and won't pop up.
2. The input mask REQUIRES you to ALWAYS enter 8 characters including the leading zeros.
3. If you click into the field rather than tab into it, you can end up past the first character and not be able to enter the date unless you back up.

I never use input masks for dates.  They also don't prevent "invalid" dates.  They only annoy me.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41771370
why not use controls input mask property, set it to something like "00/00/0000;0;_"
Yes, why not, but you'll have to support it by some code before it becomes a smart solution including validation of the input.
Here is how to do this for entering a date in the ISO sequence:
Entering ISO formatted date with input mask and full validation in Microsoft Access
By some tweaking, it could be modified to use the "reversed" US date format of mm/dd/yyyy.

/gustav
0
 

Author Closing Comment

by:Annmv888
ID: 41771619
This is what I needed.  Thanks so much for the help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question