Solved

Calendar Controls

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

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 3

Expert Comment

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now