2011-04-17 34 views
1

我试图根据输入到表单中的参数开发查询。即使用户选择不输入参数(例如开始日期和结束日期),或者用户只输入一半参数(例如,用户只输入开始日期但不结束日期或结束但不开始日期)。这就是我想要的:基于用户定义参数的IIf函数

如果开始日期和结束日期为空,则返回所有日期,

否则,如果起始日期为空,则返回日期小于或等于给定的结束日期,

否则,如果结束日期为空,则返回日期大于或等于给定的起始日期,

否则,返回指定的开始和结束日期

我想出什么之间的所有日期与似乎没有评估超越第一tru e声明,我不知道为什么。

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])))) 
+0

我认为这将是用VBA宏 – Dalen 2011-04-17 20:56:26

+0

“VBA宏”更方便的检查日期是矛盾的访问,在那里你有宏(不使用VBA)和VBA(这不是宏)。在Word/Excel中,术语是不同的。我认为在VBA中编码是什么意思。在Access中,宏并不是一个好的解决方案(直到A2010,他们在那里得到错误处理)。 – 2011-04-18 02:07:52

+0

用于什么查询?如果它是表单或报表的记录源,则不要包含任何条件,只要打开带有标准的表单/报表作为DoCmd.OpenForm/OpenReport命令的WHERE参数即可。 – 2011-04-18 02:09:01

回答

0

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]))