Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152

    Validation Rule for specific answer

    Hello,



    I have a rule to limit the response a user can input on the form.
    The code works in terms of not allowing a date prior to current year, or does not allow it to exceed current year + 20. But if a user accidently reponds then deletes and trys to move on, it states a 94 error: Null value debug.

    Code:
    Private Sub Entrance_Doors___Flats__Renew_Year__20_LE_AfterUpdate()
    If Me.[Entrance Doors - Flats (Renew Year) 20 LE] < Format(Date, "yyyy") Then
        MsgBox "Renew Year Invalid:  Less than current year!"
        End If
    If CLng(Me.[Entrance Doors - Flats (Renew Year) 20 LE] >= (Format(Date, "yyyy") + 20)) Then
        MsgBox "Renew Year Invalid:  Exceeds life expectancy!"
        End If
    End Sub
    Any help much appreciated

  2. #2
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Try assigning the value of your fields to a variable first. Sometimes this helps me.

  3. #3
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    I thought I has done this with the CLng?

  4. #4
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Code:
    Dim vEntranceDoors
    
    vEntranceDoors = [Entrance Doors - Flats (Renew Year) 20 LE]
    If vEntranceDoors < Format(Date, "yyyy") then
         'Display Messagebox
    End If
    If DatePart("yyyy", vEntranceDoors) >= (DatePart("yyyy", Date) + 20) then
         'Display Messagebox
    End If

  5. #5
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    Quote Originally Posted by jgelpi16 View Post
    Code:
    Dim vEntranceDoors
     
    vEntranceDoors = [Entrance Doors - Flats (Renew Year) 20 LE]
    If vEntranceDoors < Format(Date, "yyyy") then
         'Display Messagebox
    End If
    If DatePart("yyyy", vEntranceDoors) >= (DatePart("yyyy", Date) + 20) then
         'Display Messagebox
    End If
    Thanks - that has resolved the 94 null error.
    But has now stopped the second part (date + 20 years) from working. i can enter 3000 and it wont flag up exceeds life expectancy

  6. #6
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Try:
    Code:
    Dim vEntranceDoors
    Dim vCompareDate
    
    vCompareDate = Datpart("yyyy", Date) + 20
    vEntranceDoors = [Entrance Doors - Flats (Renew Year) 20 LE]
    
    If vEntranceDoors < Format(Date, "yyyy") then
         'Display Messagebox
    End If
    If DatePart("yyyy", vEntranceDoors) >= vCompareDate then
         'Display Messagebox
    End If

  7. #7
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    Sorry still no luck. Still allows a great entry.

  8. #8
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    What happens if you enter a less than 20 year entry? Is the text box where you enter the date the same name still?

  9. #9
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    It works fine for anything prior to current year - flags up < Current Year.
    But lets you enter anything from 2011 to infinate without error.
    Yep renamed:

    Code:
    Private Sub EntranceDoorsFlatsRenewYear_AfterUpdate()
    Dim vEntranceDoors
    Dim vCompareDate
    vCompareDate = DatePart("yyyy", Date) + 20
    vEntranceDoors = [EntranceDoorsFlatsRenewYear]
    If vEntranceDoors < Format(Date, "yyyy") Then
         MsgBox "Renew Year Invalid:  < Current Year!"
    End If
    If DatePart("yyyy", vEntranceDoors) >= vCompareDate Then
         MsgBox "Renew Year Invalid:  Exceeds Life Expectancy!"
    End If
    End Sub

  10. #10
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Ok, try...

    Code:
    Private Sub EntranceDoorsFlatsRenewYear_AfterUpdate()
    Dim vEntranceDoors
    Dim vCompareDate
    vCompareDate = DatePart("yyyy", Date) + 20
    vEntranceDoors = EntranceDoorsFlatsRenewYear.Value
    If vEntranceDoors < Format(Date, "yyyy") Then
         MsgBox "Renew Year Invalid:  < Current Year!"
    End If
    If DatePart("yyyy", vEntranceDoors) >= vCompareDate Then
         MsgBox "Renew Year Invalid:  Exceeds Life Expectancy!"
    End If
    End Sub

  11. #11
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Sorry, disregard my code on last post...I just tested code in DB, there were some syntax issues. Below code should work.

    Code:
    Dim vEntranceDoors
    Dim vCompareDate
    Dim vCurrDate
    
    vCurrDate = DatePart("yyyy", Date)
    vCompareDate = DatePart("yyyy", Date) + 20
    vEntranceDoors = DatePart("yyyy", EntranceDoorsFlatsRenewYear.Value)
    If vEntranceDoors < vCurrDate Then
         MsgBox "Renew Year Invalid:  < Current Year!"
    End If
    If vEntranceDoors >= vCompareDate Then
         MsgBox "Renew Year Invalid:  Exceeds Life Expectancy!"
    End If
    ...Guess that's what I get for not trying code out in developer prior to speaking on it....

  12. #12
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    Sorry thanks for all your help. but still not luck.

    i did have this code:
    But it falgs up all msgbox's regardless of the answer, but does stop the 94 error also.
    Code:
    Private Sub Entrance_Doors___Flats__Renew_Year__20_LE_AfterUpdate()
    If Me.[Entrance Doors - Flats (Renew Year) 20 LE] < Format(Date, "yyyy") Then
        MsgBox "Renew Year Invalid: < Current Year!"
        End If
    If Not IsNull(Me.[Entrance Doors - Flats (Renew Year) 20 LE]) Then
    If Me.[Entrance Doors - Flats (Renew Year) 20 LE] = Format(Date, "yyyy") + 20 Then
        MsgBox "Renew Year Invalid: Exceeds life expectancy!"
    End If
    End Sub

  13. #13
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Did you try the code posted at 3:31pm? I ran it and it appeared to work just fine.

  14. #14
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    Quote Originally Posted by jgelpi16 View Post
    Did you try the code posted at 3:31pm? I ran it and it appeared to work just fine.
    Yeah just tried this one and now it just flags Msgbox "Renew Year Invalid: < Current Year no matter what I enter

  15. #15
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    What entry are you expecting from users? Are you expecting a full date (i.e. 02/08/2011) or just the year? Not sure why the below code doesn't work if you're entering full years. I just tried it again and it works.

    Code:
    Dim vEntranceDoors
    Dim vCompareDate
    Dim vCurrDate
    
    vCurrDate = DatePart("yyyy", Date)
    vCompareDate = DatePart("yyyy", Date) + 20
    vEntranceDoors = DatePart("yyyy", EntranceDoorsFlatsRenewYear.Value)
    If vEntranceDoors < vCurrDate Then
         MsgBox "Renew Year Invalid:  < Current Year!"
    End If
    If vEntranceDoors >= vCompareDate Then
         MsgBox "Renew Year Invalid:  Exceeds Life Expectancy!"
    End If
    In dealing with dates you may find this link helpful.
    Format:
    http://www.techonthenet.com/access/f...ate/format.php

    DatePart:
    http://www.techonthenet.com/access/f...e/datepart.php

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Validation Rule
    By Megan in forum Access
    Replies: 1
    Last Post: 11-05-2010, 09:45 AM
  2. Validation Rule
    By Christopher in forum Forms
    Replies: 15
    Last Post: 04-20-2010, 06:05 AM
  3. Validation Rule
    By robomose in forum Access
    Replies: 1
    Last Post: 09-09-2009, 05:59 AM
  4. Validation Rule
    By smitstev in forum Access
    Replies: 5
    Last Post: 06-30-2009, 09:58 AM
  5. Validation Rule
    By mistaken_myst in forum Database Design
    Replies: 2
    Last Post: 10-29-2007, 02:08 PM

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