如何使查询条件选择所有内容如果参数为空

时间:2017-07-19 20:45:35

标签: sql ms-access

我的表单有一个名为[Supp]的组合框,它从名为[Supplier]的表格列中提取。

与此同时,我正在过滤与日期范围[Text6](开始日期)和[Text7](结束日期)

的记录相关联的日期

我正在尝试通过选择所有供应商来选择供应商过滤器,只要您离开[Supp] Blank / Null即可。

问题在于,每当我将[Supp]留空时,它似乎超过了日期范围过滤器,并选择所有可用的记录而不管日期。我认为最有可能的原因是因为我正在使用“OR”。

SQL Code

SELECT [Table Material Label].Serial, [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier, 

[Table Material Label].[Lot Number], [Table Material Label].Weight, [Table Material Label].Quantity, [Table Material Label].[Purchase Order Number], [Table Material Label].[Received By], [Table Material Label].[Checked in By], [Table Material Label].[Total Weight]

FROM [Table Material Label]

WHERE ((([Table Material Label].[Date Recieved])>=[Forms]![Report Generator]![Text6] 
    And ([Table Material Label].[Date Recieved])<=[Forms]![Report Generator]![Text7]) 
    AND (([Table Material Label].Supplier)=IIf([forms]![Report Generator]![Supp].[Value]="*",([Table Material Label].[Supplier])='*',([Table Material Label].[Supplier])=[Forms]![Report Generator]![Supp].[Value])))


ORDER BY [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier;

总结一下 - 我正在尝试创建一个按日期范围和供应商过滤的查询。但如果供应商标准为空白,我希望它不会过滤供应商。如果不破坏日期范围过滤器,我无法做到这一点。

非常感谢任何帮助。谢谢。

成品,感谢Andre的解决方案:

SELECT [Table Material Label].Serial, [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier, 

[Table Material Label].[Lot Number], [Table Material Label].Weight, [Table Material Label].Quantity, [Table Material Label].[Purchase Order Number], [Table Material Label].[Received By], [Table Material Label].[Checked in By], [Table Material Label].[Total Weight]

FROM [Table Material Label]

WHERE  (([Table Material Label].[Date Recieved])>=[Forms]![Report Generator]![Text6] 
    And ([Table Material Label].[Date Recieved])<=[Forms]![Report Generator]![Text7]) 
    AND ([Table Material Label].Supplier = [Forms]![Report Generator]![Supp] 
    OR [Forms]![Report Generator]![Supp] IS NULL)

ORDER BY [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier;

1 个答案:

答案 0 :(得分:0)

请看这里:https://stackoverflow.com/a/38899574/3820271

AND ([Table Material Label].Supplier = [Forms]![Report Generator]![Supp] 
     OR [Forms]![Report Generator]![Supp] IS NULL)

如果供应商标准为空,则此AND部分将始终为真。