Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calendar Controls

Posted on 2016-08-25
5
Medium Priority
?
77 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 39

Accepted Solution

by:
PatHartman earned 2000 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 39

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

688 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