Results 1 to 6 of 6

Thread: Date Validation

  1. #1
    Join Date
    Dec 2004
    Posts
    3

    Unanswered: Date Validation

    I am trying to stop the user from entering a date other than between Dec 1st and Dec 22nd regardless of year. Anyone know how to do that?

  2. #2
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Hi

    Would this do it for you ?
    [code]

    If Day(YourDate) > 22 or Month(YourDate)<>12 then
    Msgbox "Date is not between 1st and 22nd of December."

    end If

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    in the controls "before update" event insert your validation code
    assuming txtmydate was your control with the date variable bound to it
    the following ought to get you there

    Private Sub txtMyDate_BeforeUpdate(Cancel As Integer)
    'need to check the user has enetered something, and that it is a date
    if not isnull(txtmydate) or isdate(cdate(txtmydate)) = true then 'check its a valid date
    dim testday as integer
    if month(cdate(txtmydate))=12 then 'check its december
    testday=day(cdate(txtmydate))
    if testday<1 or testday>22 then 'check the date range
    cancel=true 'date invalid so cancel
    else
    cancel=false 'its a date between 1st & 22nd Dec so its valid
    endif
    else
    cancel=true 'not december so invalid
    endif
    else 'not a date so invalid
    cancel=true
    endif
    if cancel=true then 'if we've had an error give some feedback to the user
    msgbox ("Date must be 1st to 22nd December")
    endif
    End Sub

  4. #4
    Join Date
    Sep 2004
    Location
    Charlotte, NC
    Posts
    164
    I think the easiest thing would be to put a validation rule for the field in your table. Open your table in Design View and select the field that your date is in and select the Validation Rule and place this code in the box:
    (Month([YourDate])=12) And (Day([YourDate]) Between 1 And 22)
    but replace 'YourDate' with the name of your field. Then in the Validation Text box put "The Date must be between December 1st and 22nd."

    TD

  5. #5
    Join Date
    Dec 2004
    Posts
    3

    Thanks buckeye_td

    thanks for the help I figured that I would have to use the Month and Day functions

  6. #6
    Join Date
    Dec 2004
    Posts
    3

    Thanks buckeye_td

    thanks for the help I figured that I would have to use the Month and Day functions

Posting Permissions

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