Results 1 to 10 of 10
  1. #1
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45

    Question Help Please in Form Yes/no field

    Hello, please I need help on this, is there any way to update a yes/no field when a number in another field change?



    For example, I have a table with a 4 fields, 2 are Field Dates, another is the difference of this to date that could be negative or positive, and the other field is name EXPIRED(yes/No), that I want is when the difference in 3 field is negative the field is YES, and when is positive is NO in the EXPIRED field, I get the change but just in afterupdate, but I need this automatic when the number change

    Private Sub Expired_BeforeUpdate(Cancel As Integer)
    If Me.Days_Ramaining.Value < 0 Then
    Me.[Expired] = True
    End If
    Else
    If Me.Days_Ramaining.Value > 0 Then
    Me.[Expired] = False
    End If
    End Sub

    THanks

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Move the code to the AfterUpdate event of the Days_Remaining field

    BUT:
    a) what if Days_Remaining = 0?
    b) you can simplify the code as follows

    Code:
    Private Sub Days_Ramaining_AfterUpdate()
    If Me.Days_Ramaining < 0 Then 'or use <=0??? see comment above
         Me.Expired = True
    Else
         Me.Expired = False
    End If
    End Sub
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I wouldn't actually store this - just calculate it. Same with your Days_Remaining field, you can always calculate it - therefore don't store it.
    As you have found storing it means you have to try and capture any changes made anywhere in your system to keep it accurate. Simply calculate it in your forms underlying query and it will always be accurate.

    Two calculated fields in your forms query;

    DaysRemainCalc : DateDiff(["d",StartDate],[EndDate])
    ExpiredCalc: IIf(DateDiff(["d",StartDate],[EndDate]) > 3, True, False)
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45
    Thanks, I try this but do not update the Expired Field

  5. #5
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45
    Private Sub Days_Ramaining_AfterUpdate()
    If Me.Days_Ramaining < 0 Then 'or use <=0??? see comment above
    Me.Expired = True
    Else
    Me.Expired = False
    End If
    End Sub

    this do not work

  6. #6
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45
    Quote Originally Posted by Minty View Post
    I wouldn't actually store this - just calculate it. Same with your Days_Remaining field, you can always calculate it - therefore don't store it.
    As you have found storing it means you have to try and capture any changes made anywhere in your system to keep it accurate. Simply calculate it in your forms underlying query and it will always be accurate.

    Two calculated fields in your forms query;

    DaysRemainCalc : DateDiff(["d",StartDate],[EndDate])
    ExpiredCalc: IIf(DateDiff(["d",StartDate],[EndDate]) > 3, True, False)
    Hello, to try this where can I must put this codes? in the query criteria
    thanks for help

  7. #7
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45
    Quote Originally Posted by Minty View Post
    I wouldn't actually store this - just calculate it. Same with your Days_Remaining field, you can always calculate it - therefore don't store it.
    As you have found storing it means you have to try and capture any changes made anywhere in your system to keep it accurate. Simply calculate it in your forms underlying query and it will always be accurate.

    Two calculated fields in your forms query;

    DaysRemainCalc : DateDiff(["d",StartDate],[EndDate])
    ExpiredCalc: IIf(DateDiff(["d",StartDate],[EndDate]) > 3, True, False)

    Thanks with some change I did this way work perfect... thanks all guys for your help

  8. #8
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45
    HEllo, now I saw that if end date is empty, the expired is also true, is there any way if expiration date is null, the expired field become false too? this is the code I use:

    Expr2: IIf(DateDiff("d",Date(),[Expiration Date])>0,False,True)

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Try this

    Code:
    Expr2: IIf((DateDiff("d",Date(),[Expiration Date])>0) Or (Nz([Expiration Date],0)=0),False,True)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  10. #10
    christ2000 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    45

    Thumbs up

    Quote Originally Posted by ridders52 View Post
    Try this

    Code:
    Expr2: IIf((DateDiff("d",Date(),[Expiration Date])>0) Or (Nz([Expiration Date],0)=0),False,True)

    Work Perfect, Thanks For your help to all of U

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

Similar Threads

  1. Replies: 6
    Last Post: 07-28-2017, 01:36 AM
  2. Replies: 17
    Last Post: 03-16-2014, 10:52 PM
  3. Replies: 3
    Last Post: 12-27-2013, 02:33 PM
  4. Replies: 2
    Last Post: 03-10-2013, 01:03 AM
  5. Replies: 2
    Last Post: 07-14-2011, 09:23 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