Results 1 to 9 of 9
  1. #1
    smc678 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    65

    iif Statement Help


    I am trying to write an iff statement for iff [Time Arrived] = 0400-1459 it would display 1 or [Time Arrived] = 1500-0359 it would display 2.

    any Ideas?

  2. #2
    hawkdriver's Avatar
    hawkdriver is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    23
    I am not sure what your fields are like, but it seems fairly straight forward.

    IIF([table].[Time Arrived]="0400-1459", "1", IIF([table].[Time Arrived]="1500-0359", "2", Nothing))

    The thing that I don't know is whether your 0400-1459 is a string or not.

    Another way to do this is to set the 0400-1459 as a value or the 1 you refer to in your question. Just create a table that has your values listed. Key your 1 value equal to 0400-1459 and 2 to 1500-0359. Then your IIF works well.

    That's the beauty with databases, they are very flexible in design.

  3. #3
    smc678 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    65
    Thank you for your response. It comes up with a #name? This is put within a form and i placed this expressing in the default value is that right?

  4. #4
    smc678 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    65
    and the 0400-1459 is a in between military time i dont know if you knew that or not.


    Click image for larger version. 

Name:	Capture1.PNG 
Views:	27 
Size:	22.3 KB 
ID:	10227

  5. #5
    hawkdriver's Avatar
    hawkdriver is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    23
    Post your code and I will help you get this resolved. Where are you wanting the 1 displayed? I understand you are wanting to translate the 0400-1459 to a 1, but where is this happening and how do you want the IIF to do this. I am only seeing a partial indication of what you want. If you are only doing two choices, why don't you just put a selection in a drop down and send the value back to the table using the 1 or 2? It looks like you have a text box above and there are easier methods to accomplish your goal. I just need a starting place to help.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    iff [Time Arrived] = 0400-1459 it would display 1 or [Time Arrived] = 1500-0359 it would display 2.
    You cannot do a comparison like that. You understand what it means, but Access would try to subtract the two numbers, then compare the result to the time or treat it like a text field, comparing a date/time type to a text string.

    First, it is IIF, not IFF. (computers can never spell correctly)
    Second, you should only use letters, numbers and the underscore in object names. No spaces.
    Next, you have to check if a time is less than the time field, then check if the time field is greater than a time (or use the beween keyword).

    I would put this in the query for the form, but you can use it in the control source of the control.

    For a control source:
    Code:
    =IIf([TimeArrived]>=TimeValue("4:00 AM") And [TimeArrived]<=TimeValue("02:59 PM"),1,2) 
    
    or
    
    = IIf([TimeArrived] Between TimeValue("4:00 AM") And TimeValue("02:59 PM"),1,2)

    In a query, you would use
    Code:
    Shift: Shift: IIf([TimeArrived]>=TimeValue("4:00 AM") And [TimeArrived]<=TimeValue("02:59 PM"),1,2)
    Or
    Code:
    ShiftCalc: IIf([TimeArrived] Between TimeValue("4:00 AM") And TimeValue("02:59 PM"),1,2)
    ------
    Also, if the field "Transaction" has meaning, it shouldn't be an automunber type. You should not use an Autonumber if the field is meant to have meaning for the users.
    Autonumbers are not guaranteed to be sequential, consecutive or even positive. They are only guaranteed to be unique.
    If you need a sequential, consecutive & positive number, you will have to "roll your own". There are many examples to do this.
    Last edited by ssanfu; 12-04-2012 at 07:54 PM. Reason: Corrected times

  7. #7
    hawkdriver's Avatar
    hawkdriver is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    23
    I will never understand people that try to teach by being "superior" to others. An IIF could absolutely work by comparing to literal text values. The problem remains that we are guessing his/her intentions. That's why I asked for more information.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @hawkdriver

    If I have hurt your feelings, sorry. I do not think I am, nor am I trying to be "superior to others". Should I have been more verbose in my post? Apparently so.

    Having read your post and the OP's reply, I made a table, added 8 records, created a query and pasted in your solution. It didn't work. I tried several ways to modify your expression, but couldn't get the required result. So I went another direction that seemed much simpler.

    I could have created a UDF to do the comparison, but felt that was overkill when a simpler expression would suffice. I tested my expressions, posted both.


    An IIF could absolutely work by comparing to literal text values
    Yes, but it would not have been simple to do it.

    The problem remains that we are guessing his/her intentions
    Maybe... but it seemed clear to me, so I posted my solution.

    If I post a solution that I have not tested, I always try to add "Air Code" or "Warning - untested" so the OP understands there might be problems with my solution.


    I will try to be more clear in my writing in the future.

    Steve
    --------------------------------
    "Veni, Vidi, Velcro"
    (I came; I saw; I stuck around.)

  9. #9
    hawkdriver's Avatar
    hawkdriver is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    23
    No feelings hurt guy. Thanks for the more loquacious response. I was referring to your
    First, it is IIF, not IFF.
    , to which you later added
    (computers can never spell correctly)
    Regardless, thanks for trying.

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

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  3. If Statement
    By ajolson1964 in forum Access
    Replies: 1
    Last Post: 05-11-2011, 07:51 AM
  4. need a If /then statement
    By kmiszczak in forum Access
    Replies: 1
    Last Post: 04-12-2011, 01:51 PM
  5. If Then Statement Help
    By Kapelluschsa in forum Programming
    Replies: 5
    Last Post: 08-11-2010, 09:24 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