Calculated Query with IIF, AND & ELSE Conditions (1 Viewer)

Reese

Registered User.
Local time
Today, 01:55
Joined
Jan 13, 2013
Messages
387
[Solved] Calculated Query with IIF, AND & ELSE Conditions

So, there may already be an answer for this somewhere but I can't find it.

I know it's possible to create a calculation query that uses different calculations based upon different variables using an IIF statement. I've done it before using the following and it works fine:

Code:
(IIf([Cost_Category]="Full Price",Sum(nz([Program_Cost])+nz([Millage_Fee])+nz([Auditorium_Cost])+nz([Cancel_Fee])),IIf([Cost_Category]="Discount",Sum(nz([Program_Cost])+nz([Millage_Fee])+nz([Auditorium_Cost])+nz([Cancel_Fee])),IIf([Cost_Category]="Swap",Sum(nz([Program_Cost])+nz([Millage_Fee])+nz([Auditorium_Cost])+nz([Cancel_Fee])),IIf([Cost_Category]="Donation",Sum(nz([Program_Cost])+nz([Millage_Fee])+nz([Auditorium_Cost])+nz([Cancel_Fee])),IIf([Cost_Category]="BOCES",Sum(nz([Cost_Per_Person])*nz([BOCES_Number_of_Participants])))))))) AS Total_Cost
I would like to know if I can integrate AND and ELSE functions into the SQL at the same time. I have two goals in this:

-Simplify the above SQL so it's along the lines of IIF([Cost_Category]="BOCES",Sum( insert calculation here), ELSE Sum( insert different calculation here) AS Total_Cost

-Create a new query that has something along the lines of IIF([Canceled]="-1" AND [Paid]="0",( set Total_Cost as "0"), ELSE Sum( insert calculation here) AS Total_Cost

Is something like this possible? Thanks!
 
Last edited:

Reese

Registered User.
Local time
Today, 01:55
Joined
Jan 13, 2013
Messages
387
Ok, thanks. I'll play around with it and see what I can do.
 

Reese

Registered User.
Local time
Today, 01:55
Joined
Jan 13, 2013
Messages
387
Thanks, David. I got the following code to work great:

Code:
(IIf(([Canceled]="-1" And [Paid]="0"),"0",Sum(nz([Program_Cost])+nz([Millage_Fee])+nz([Auditorium_Cost])+nz([Cancel_Fee])-nz([Reimbursed_Amount])))) AS Total_Cost

The only problem is now Access doesn't seem to allow formatting of the Actual_Cost field--i.e. I lost the currency formatting and when I look at the field's properties in Design view, the Format drop-down menu is completely blank.

The same thing happened when I simplified the query that I posted at the beginning of this thread (which, also calculated everything perfectly). Any idea why that is and how I can fix it?
 

David R

I know a few things...
Local time
Today, 00:55
Joined
Oct 23, 2001
Messages
2,633
Yup. Since you had "0" in your example I figured you were using a string variable for some reason.

Glad you got it working!
 

Users who are viewing this thread

Top Bottom