Results 1 to 7 of 7
  1. #1
    Bugsy is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2011
    Posts
    4

    IIf in Validation Rule


    I have a form that is used for initial entry of data as it is captured. On that form is a field called PROVCODE that is a pick-list from a provider dictionary with a bound column of the provcode value from the provdict table (e.g. - J07). On that same form a few fields down there is a DOS (date of service) text box. It is this box I want to add the validation rule to. Originally the validation rule was <=Date() so that the user couldn't enter a DOS value as a future date. Now I need to change this rule so that if the PROVCODE value is J07 that the validation rule should be (<=Date()-21) (meaning the value being entered must be at least 21 days ago in the past) otherwise is should be <=Date(). I thought it would be simple, but cannot get it to work in the Expression builder for the DOS field in the form.

    I am not a VBA programmer.

    Any ideas?

    Thx

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I don't think the validation expression can use IIf, seems I tried that. Will have to programmatically handle the validation in VBA. Two approaches.

    1. Use the PROVCODE box AfterUpdate event. Code would modify the Validation property of DOS box.

    2. Use the BeforeUpdate event of DOS box to validate entry based on value in PROVCODE. Validation property not used.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Bugsy is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2011
    Posts
    4
    June7:

    Thx, but as I don't "do" vba can you give me some idea as to what this code would look like?

    Many thanks.

  4. #4
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Following June7's advice:

    Go to the property sheet of Provcode and select the afterupdate property. Click on the 3 dots and select "Code Builder".
    The Visual Basic screen will appear with the event handler
    Sub Provcode_afterupdate()
    End Sub

    Insert:

    Dim S as string
    If [Provcode] = "J07" then
    S = "<= #" & StringDate(Dateadd("d",-21,today())) & "#"
    else
    S = "<= # & StringDate(Today()) & "#"
    end if
    [DOS].validationrule = S

    This all depends on how the date field is defined in DOS. I suggest you test by manually inserting a string into the Validation Rule, followed by making sure that the above code gets it right.

  5. #5
    Bugsy is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2011
    Posts
    4
    hertfordkc:

    Thanks for taking the time to help. I am getting an error message (Compile Error: sub or functuion not defined) on the frst instance of "today". I also think I may be mssing a " in the Else portion, but got the same error when I added one.

    Again, thanks for any insights you can share.

    Best Regards

  6. #6
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    This will compile

    Quote Originally Posted by hertfordkc View Post
    Go to the property sheet of Provcode and select the afterupdate property. Click on the 3 dots and select "Code Builder".
    The Visual Basic screen will appear with the event handler
    Sub Provcode_afterupdate()
    End Sub

    Insert:

    Dim S as string
    If [Provcode] = "J07" then
    S = "<= #" & Dateadd("d",-21,Date) & "#"
    else
    S = "<= #" & Date) & "#"
    end if
    [DOS].validationrule = S

    This all depends on how the date field is defined in DOS. I suggest you test by manually inserting a string into the Validation Rule, followed by making sure that the above code gets it right.
    My apologies for the error

  7. #7
    Bugsy is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2011
    Posts
    4
    THANKS!

    No apology necessary. Your help proved invaluable.


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
    By rbiggs in forum Forms
    Replies: 4
    Last Post: 08-23-2011, 05:24 PM
  3. Validation Rule Help!
    By confidego in forum Access
    Replies: 8
    Last Post: 07-20-2011, 12:54 PM
  4. Validation Rule
    By Megan in forum Access
    Replies: 1
    Last Post: 11-05-2010, 09:45 AM
  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