Results 1 to 13 of 13
  1. #1
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124

    validation rule for dates

    Hi
    I have two dates in my form, Date A (date of birth) and Date B (date of a process), both dates are in same table and form called persondetails.
    What will be the validation rule if I want:
    1. Date A must be either equal to Date B or earlier than Date B

    2. Date A must be today or earlier

    3. Date B must be today or earlier



    I know for second and third we use <Now() but I am struggling to find a full rule for all of the above conditions.

    Thanks

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    1. Date A must be either equal to Date B or earlier than Date B

    2. Date A must be today or earlier

    3. Date B must be today or earlier

    use a small vba procedure
    ___UNTESTED ___
    Code:
    If isDate(DateA) and
      IsDate(dateB) and
      DateA<=DateB and
      DateB <= Date  Then           '  (or Now whatever your conditions require)
    do stuff here
    else
    Error condition
    endif
    Last edited by orange; 10-01-2012 at 06:32 AM. Reason: spelling

  3. #3
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    Hi Orange
    Can you please give a complete code as I am trying to put this code but its not working.
    Specially cant understand this part
    DateB <= Date Then ' (or Now whatever your conditions require)
    do stuff here

    Thanks

  4. #4
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    I was using this code but its giving expression error.

    If IsDate(DOB)
    IsDate Dateofoperation
    DOB =<Dateofoperation And
    Dateofoperation=<Date
    DOB <= Date
    Then DoCmd.Save acTable
    Else
    MsgBox "DOB can't be later than Date of operation", [vbOKOnly], [Error]
    End If
    End Sub

    DOB is date A
    Dateofoperation is date B

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Try the before update event and adapt orange's code:

    http://www.baldyweb.com/BeforeUpdate.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Something like the following, but as Paul suggested, you should use the BeforeUpdate event

    Code:
    If IsDate(DOB)  AND _ 
       IsDate( Dateofoperation) AND _
       DOB =<Dateofoperation And   _ 
          Dateofoperation=<Date AND _
          DOB <= Date  Then 
       DoCmd.Save acTable
    Else
        MsgBox "DOB can't be later than Date of operation", [vbOKOnly], [Error]
    End If
    End Sub

  7. #7
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    Hi
    I have used this code:

    Private Sub Text130_BeforeUpdate(Cancel As Integer)
    If IsDate(DOB) And _
    IsDate(Dateofoperation) And _
    DOB <= Dateofoperation Then
    DoCmd.Save acDefault

    Else
    MsgBox "Date of operation cannot be earlier than DOB"

    End If
    End Sub

    I have used it as you said in before update event, and it is working fine uptill this msgbox but as I click Ok on msg box the cursor moves to next field in the form, rather than staying on the same field to correct it. I am also using a validation rule for the same field for entering today's or earlier date which is working and does not allow user to move forward unless user enter correct date. What can I add in this code so that either the text130 get refreshed after the appearence of the msgbox if user enters wrong date.

    thanks

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  9. #9
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    Hi
    Yeah it was there, but for "Date to be today or earlier", instead of code I have used validation rule, which is working fine.

  10. #10
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    But my problem is as I said the focus moves on after the msgbox appears and user clicks OK. Which should not be the case, it should bound the user to enter proper date in the field and for that, I am asking a code which either refresh/requery the Text box or keep on displaying the error.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You don't have the

    Cancel = True

    line which cancels the update and leaves focus on the control.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    ashu.doc is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    124
    Ok finally that works. thanks ever so much.

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Validation Rule
    By Darkladymelz in forum Reports
    Replies: 2
    Last Post: 09-15-2011, 07:43 AM
  2. Validation Rule Help!
    By confidego in forum Access
    Replies: 8
    Last Post: 07-20-2011, 12:54 PM
  3. Validation Rule
    By ritzzy in forum Access
    Replies: 1
    Last Post: 04-13-2011, 01:33 PM
  4. Help with validation rule
    By Desstro in forum Programming
    Replies: 5
    Last Post: 02-27-2011, 08:05 PM
  5. Validation Rule
    By smitstev in forum Access
    Replies: 5
    Last Post: 06-30-2009, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums