Results 1 to 10 of 10
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    If statement with calculated date field

    I have an if statement that isn't working. I calculated a date from another query. It is written like this:
    ExpirDate: [DateofTraining]+[DaysToExpir]

    I had to use that field [Expir in another query to see if there was an expiration date or not:
    ExprDate: IIf(([DateofTraining]=[ExpirDate]),"No Expiration",[ExpirDate])

    When I tried to use that field [ExprDate] in another query, it didn't work right:
    Expired: IIf(([ExprDate]<Date()),"EXPIRED","")

    I got the opposite effect I was trying for:
    Click image for larger version. 

Name:	query.jpg 
Views:	16 
Size:	164.0 KB 
ID:	33285

    I know that I nested queries, but figured this would work. Everything else seemed to work right. Any ideas?

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    If [ExprDate] is the result of the expression IIf(([DateofTraining]=[ExpirDate]),"No Expiration",[ExpirDate]), then the result must be a variant because it is either "No Expiration" or it appears to be a date: [ExpirDate]. Certainly, if the result is No Expiration, it cannot be less than <Date(). That would be an invalid comparison, thus it defaults to the part of the IIF statement that is to be chosen if the first part is False, which is EXPIRED
    Last edited by Micron; 03-25-2018 at 07:06 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Then wouldn't all of them be EXPIRED?

  4. #4
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Possible solution?

  5. #5
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    OK. Figured out a solution. Used a query that didn't have that particular if statement. Thanks.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Quote Originally Posted by UT227 View Post
    Then wouldn't all of them be EXPIRED?
    what if this IIf(([DateofTraining]=[ExpirDate]),"No Expiration",[ExpirDate] resulted in a valid date? Wouldn't subsequent comparisons to Date() be valid?
    It's my understanding that this ExpirDate: [DateofTraining]+[DaysToExpir] is a valid date calculation. Perhaps I am not correctly deciphering the outcomes from all of your expressions.

  7. #7
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    That is the if statement that I used to get "No Expiration". It works great.
    ExprDate: IIf(([DateofTraining]=[ExpirDate]),"No Expiration",[ExpirDate])
    However, when I try to add an if statement to show that the ExprDate is < Date(), that's when it stops working.
    Expired: IIf(([ExprDate]<Date()),"EXPIRED","")
    I get it showing that the dates that are not expired showing as expired.

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    I think you are missing the point. Let's assume this results in a valid date
    ExpirDate: [DateofTraining]+[DaysToExpir], so 03/26/2018 as an example

    This ExprDate:IIf(([DateofTraining]=[ExpirDate]),"No Expiration",[ExpirDate]) should produce 1 of 2 outcomes
    ExprDate is either "No Expiration" OR a date, and I've already determined that the date is 03/26/2018

    Then this Expired:IIf(([ExprDate]<Date()),"EXPIRED","") is either saying
    Expired:IIf((03/26/2018<Date()),"EXPIRED","") which should be fine as we can compare any date to Date()
    but if ExprDate is "No Expiration" you cannot compare that to any date, thus your query will produce incorrect results. How can the text No Expiration be compared to any date? It matters not that your prior expression evaluates as you expect if subsequent uses of it's values don't.

    If I'm the one who is missing something here then I apologize and appreciate your patience.

  9. #9
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I might be missing the point. I understand why it doesn't work. I was wondering if that had affected the outcome of the query. I agree that it did. I changed it so that it now doesn't use that if statement. I have it working so that I'm showing the correct dates being shown as expired. Now, I have another problem that popped up. Because those dates that have no expiration are equal to the [DateofTraining], they show up as being expired because they fit the statement as being <Date(). Now, I'm trying to find a way to filter those out.

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Seems to me that if we are to continue you should post a sample of data along with what the outcome should be. A zipped copy of your db would probably be even better.

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

Similar Threads

  1. Replies: 1
    Last Post: 11-27-2017, 02:02 PM
  2. Replies: 5
    Last Post: 07-02-2014, 07:13 AM
  3. Replies: 8
    Last Post: 06-09-2014, 01:14 PM
  4. If then statement on calculated field
    By blakej in forum Access
    Replies: 9
    Last Post: 03-17-2014, 01:12 PM
  5. Replies: 3
    Last Post: 04-01-2012, 01:40 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