IIf Function Based on User Defined Parameters

937 Views Asked by At

I'm trying to develop a query based on parameters entered into a form. I want to make sure that the query will still work even if the user chooses not to enter parameters (ex. start and end date) or if the user only enters half of the parameters (ex. user only enters start but not end date or end but not start date). This is what I want:

If start date and end date is null, then return all dates,

else, If start date is null, then return dates less than or equal to the given end date,

else, If end date is null, then return dates greater than or equal to the given start date,

else, Return all dates between the given start and end date

What I've come up with doesn't seem to evaluated beyond the first true statement and I'm not sure why.

IIf(IsNull([forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date]
And [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date]),  
[Volunteer_Date],  

IIf(IsNull([forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date]),  
<= [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date],  

IIf(IsNull([forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date]),  
>= [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date],  

(Between [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date] 
And [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date]))))
1

There are 1 best solutions below

0
On

Got it!

WHERE (IIf(IsNull([Forms]![Volunteer_Hours_By_Person]!      [Volunteer_Hours_By_Person_Name_Selection]),(People.PID) like "*",(People.PID)=[Forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Name_Selection]))

AND (IIf(IsNull([forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date]) AND IsNull([forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date]),(Volunteer.Volunteer_Date) like "*",([Volunteer].[Volunteer_Date]) Between [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_Start_Date] And [forms]![Volunteer_Hours_By_Person]![Volunteer_Hours_By_Person_End_Date]))