Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2004

    Unanswered: Date Field restriction

    If I wish to allow a user to enter the year 2007 only into a date field, what code would I use in the event procedure to accomplish that?

    Thanks, in advance, for any suggestions.

  2. #2
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    put some validation code behind the the control or forms before update event

    if cint(<mycontrolname>) <> 1997 then
    cancel = true

    Id suggest you put some validation in the control to limit input to numbers.. check the controls properties and set an appropriate format mask

    you may care to put in a default value when you insert a new record in the forms on insert event
    <mycontrolname> = year(now())
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jun 2005
    Richmond, Virginia USA
    Provided Answers: 19
    If you're saying you want the year portion of the date to be 2007, as opposed to having only the year in the field, then something like this:

    Private Sub YourDate_BeforeUpdate(Cancel As Integer)
    If Year(YourDate) <> 2007 Then
     Cancel = True
     MsgBox "Year For This Date Must Be 2007!"
    End If
    End Sub
    To limit the year portion of the date to the current year:

    Private Sub YourDate_BeforeUpdate(Cancel As Integer)
    If Year(YourDate) <> Year(Date) Then
     Cancel = True
     MsgBox "Date Must Fall In Current Year!"
    End If
    End Sub
    As Healdem said, you should also probably do some validation to assure that a valid date is input.
    Hope this helps!

    The problem with making anything that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Join Date
    Jan 2007
    Provided Answers: 10
    Why not simply limit the user to selecting the day and month, then parsing the values with the 2007 on the end?
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts