Solved

Calendar Controls

Posted on 2016-08-25
5
38 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
5 Comments
 
LVL 35

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 35

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 49

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Allow user to edit an outgoing email 2 25
Column Layout in Access Xp VBA Report 3 19
Access Delete Query Not Working 13 32
Dcount help 2 16
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

810 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