Saturday, September 7, 2013

Replace IIf with SWITCH in WHERE clause of MS-Access 2003 query

Replace IIf with SWITCH in WHERE clause of MS-Access 2003 query

I have the following query in MS-Access 2003 and it works OK:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent,
tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf
([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));
I wish to replace the IIf function with the Switch function but whatever I
tried didn't work. My best approach is the following:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent,
tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH
([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,
[qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,
[qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,
[qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));
but I get a message
Type mismatch in expression
Please advice!

No comments:

Post a Comment