Results 1 to 3 of 3
  1. #1
    GonzaloYagueSunol is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Sep 2015
    Posts
    6

    update a field with a certain value if a condition is met

    Dear all,



    This is the original table and it is called "Dates":

    Initial Date Final Date Days Permission
    20/08/2015 22/08/2015
    12/08/2015 30/08/2015
    20/08/2015 18/08/2015

    On one hand, I want to update the "Days" field with the difference (number of days) between "Final Date" and "Initial Date". On the other hand, if "Days" is greater than 15 "Permission" will be "Green", if it is between 15 and 0 "Permission" will be "Amber" and if it is lower than 0 "Permission" will be "Red". The desired result is shown below:

    Initial Date Final Date Days Permission
    20/08/2015 22/08/2015 2 Amber
    12/08/2015 30/08/2015 18 Green
    20/08/2015 18/08/2015 -2 Red

    In order to achieve it, I am trying to run an update query. With the objective of making it easier, initially I just want Permission to be equal to Green if "Days">15. This is what I have done so far:


    Field Days Permission
    Table Dates Dates
    Update to DateDiff("d",[Initial Date],[Final Date]) "Green"
    Criteria DateDiff("d",[Initial Date],[Final Date])>15

    I obtain the number of days but unfortunately "Permission" appears as "Green" always.

    Thank you very much in advance for your help.

    Best regards,

    Gonzalo

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    make it 2 queries, 1, set the days
    Q1 to update the days , DateDiff("d",[Initial Date],[Final Date])

    'then set the colors now that you have days.
    to update [permisison] using the [Days] ,

    Q2, update table set [permisison] = "Green" where [days]>15
    Q3, update table set [permisison] = "RED" where [days]<16



  3. #3
    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,848
    Why do you need this in a table?
    You can calculate the number of days via a query. And you can determine if the difference is >15
    Code:
      If (Datediff("d",[Initial Date],[Final Date])) > 15 Then
           'do something since > 15 days
           Permission = "Green"
        ElseIF (Datediff("d",[Initial Date],[Final Date])) <0 Then
          'do something else since <0 days [negative]
           Permission = "Red"
        Else
           Permission = "Amber"   '>=0 and <= 15
       End If

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

Similar Threads

  1. update records if condition met!
    By TallSA in forum Queries
    Replies: 5
    Last Post: 08-17-2015, 07:57 AM
  2. Adding Value to a field when condition is met
    By Gabynex9 in forum Access
    Replies: 14
    Last Post: 05-21-2015, 08:21 PM
  3. Condition Formatting at After Update event (TextBox)
    By excellenthelp in forum Programming
    Replies: 8
    Last Post: 06-20-2014, 02:54 PM
  4. Update with if condition
    By drunkenneo in forum Queries
    Replies: 1
    Last Post: 03-27-2014, 02:16 PM
  5. Update 2 fields based on where condition.
    By Confused in forum Access
    Replies: 2
    Last Post: 11-19-2009, 05:21 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