Solved

Calendar Controls

Posted on 2016-08-25
5
49 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 36

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 36

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 50

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

756 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