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

    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
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    put some validation code behind the the control or forms before update event

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

    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
    Location
    Richmond, Virginia USA
    Posts
    2,763
    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:

    Code:
    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:

    Code:
    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 foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    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?
    George
    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
  •