IIf statement with multiple conditions

Steve_T

Registered User.
Local time
Today, 00:07
Joined
Feb 6, 2008
Messages
96
Hi,
I have had a look at other posts on which i have attempted to create a IIf statement in a Query but it will not run. It shows a error message saying about the bracketing or a syntex error.
I would be grateful if someone could please have a look at it and lend some knowledge to me as i am a complete newby to them.

First of all let me show what fields i am using from my Table:

Mon_Hrs (this shows hours being worked as shown on rota)
Mon_Reason (this can be either blank or show a 2 digit absence code)
(PH = Part holiday which is rota hours and break time depending on whether full or part time, other reason codes will mean 0 hours worked that day)

MONDAY_HOURS: IIF(IsNull([Mon_Reason]),[Mon_Hrs],IIf([Mon_Reason] = "PH" And [Mon_Hrs] = 7.5,4,IIf([Mon_Reason] = "PH" And [Mon_Hrs] = 5,2.5,0)))

This is my attempt above and i know it will be either something silly or a complete mess and not workable but i would be grateful is someone could help me.

Thanks
 
I have just tried your iif statement in a test database and it seems to be working perfectly.
 
Hi,
I have had a look at other posts on which i have attempted to create a IIf statement in a Query but it will not run. It shows a error message saying about the bracketing or a syntex error.
I would be grateful if someone could please have a look at it and lend some knowledge to me as i am a complete newby to them.

First of all let me show what fields i am using from my Table:

Mon_Hrs (this shows hours being worked as shown on rota)
Mon_Reason (this can be either blank or show a 2 digit absence code)
(PH = Part holiday which is rota hours and break time depending on whether full or part time, other reason codes will mean 0 hours worked that day)

MONDAY_HOURS: IIF(IsNull([Mon_Reason]),[Mon_Hrs],IIf([Mon_Reason] = "PH" And [Mon_Hrs] = 7.5,4,IIf([Mon_Reason] = "PH" And [Mon_Hrs] = 5,2.5,0)))

This is my attempt above and i know it will be either something silly or a complete mess and not workable but i would be grateful is someone could help me.

Thanks

Nothing about this statement jumps out at me as incorrect, although there are some aspects that i might have done differently. I have reformatted the IIf query and displayed it below with the changes (marked in red), but for the record, I do not know how (or why) any of the changes would help you to resolve your issue, since I do not see anything wrong.

Code:
[COLOR=black][FONT=Verdana]MONDAY_HOURS: [/FONT][/COLOR]
[COLOR=black][FONT=Verdana]II[B][COLOR=red]f[/COLOR][/B](IsNull([Mon_Reason]),[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       [Mon_Hrs],[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       IIf([B][COLOR=red](([/COLOR][/B][Mon_Reason] = "PH"[B][COLOR=#ff0000])[/COLOR][/B] And [B][COLOR=#ff0000]([/COLOR][/B][Mon_Hrs] = 7.5[COLOR=red][B]))[/B][/COLOR],[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]               4,[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]               IIf([B][COLOR=#ff0000](([/COLOR][/B][Mon_Reason] = "PH"[B][COLOR=#ff0000])[/COLOR][/B] And [B][COLOR=#ff0000]([/COLOR][/B][Mon_Hrs] = 5[B][COLOR=#ff0000]))[/COLOR][/B],[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                       2.5,[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]                       0[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]               )[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]       )[/FONT][/COLOR]
 
Hi Rabbie and MSAccessRookie,
I am so sorry for wasting your time, as stupid as it seems when i wrote the question on here i corrected it without realizing. My error was that when i created the query and did not use "" on the lettering.
Once again so sorry, just goes to show sometimes it pays to step away, have a coffee and comeback.
 
No probs. At least it's working now.
 
I think that this thread illustrates one of the rules of programming.
Syntax errors are the most common and yet the most difficult to find as we read what we expect to see.


If Steve had copied and pasted the query he would be kicking himself for his "stupid" error, but its not stupid , its common we've all been there and don't look on it as having had our time wasted.

Brian
 
Brianwarnock,
Thanks for the statement, i looked and looked at the IIf statement and never saw it once, it was driving me mad. Even split it up to try and find the error. This is another example of the support and understanding i have always received here at AWF. No matter how silly or complex a question is you can guarantee someone will try and help or if not direct you to a thread / link that can.
 
Brianwarnock,
Thanks for the statement, i looked and looked at the IIf statement and never saw it once, it was driving me mad. Even split it up to try and find the error. This is another example of the support and understanding i have always received here at AWF. No matter how silly or complex a question is you can guarantee someone will try and help or if not direct you to a thread / link that can.
Steve you seem to be forgetting that you actually posted correct working code that did what you wanted. :)
 
Hi -

You also might consider the Switch() function. Saves the formatting hassle, matching parenthesis, etc..

MONDAY_Hours = Switch(IsNull([Mon_Reason]), [Mon_Hrs], [Mon_Reason] = "PH" AND [Mon_Hrs] = 7.5, 4, [Mon_Reason] = "PH" And [Mon_Hrs] = 5, 2.5, True, 0)

Bob
 
Hi -

You also might consider the Switch() function. Saves the formatting hassle, matching parenthesis, etc..

MONDAY_Hours = Switch(IsNull([Mon_Reason]), [Mon_Hrs], [Mon_Reason] = "PH" AND [Mon_Hrs] = 7.5, 4, [Mon_Reason] = "PH" And [Mon_Hrs] = 5, 2.5, True, 0)

Bob

Bob,
I can see the benefits of using this over an IIf statement,so thanks for suggesting it.

Steve
 

Users who are viewing this thread

Back
Top Bottom