简体   繁体   English

MS Access-使用表单输入过滤查询中的多个条件

[英]MS Access - Using Form Inputs to Filter on Multiple Criteria in a Query

In MS Access, I want to use a Form to drive the multiple filtering criteria for a query. 在MS Access中,我想使用表单来驱动查询的多个筛选条件。 In each of the combo boxes, I added null values for users to leave blank as not all criteria need a specified value 在每个组合框中,我为用户添加了空值,以便用户留空,因为并非所有条件都需要指定值

Form 形成

Currently, I am able to filter on the first field [Type of Agreement], but cannot get the Sponsor field to work. 目前,我可以在第一个字段[协议类型]上进行过滤,但是无法使发起人字段起作用。 This prevents me from even trying to filter based on the other fields. 这使我什至无法尝试根据其他字段进行过滤。 As a starting point, this is what I have: 首先,这就是我所拥有的:

SELECT TestInPut.[Type of Agreement], TestInPut.Sponsor, TestInPut.[Proposal Title], TestInPut.Div,  TestInPut.[Award Status], 
FROM TestInPut
WHERE ((TestInput.[Type of Agreement])=Forms!Console!SelectType) Or ((Forms!Console!SelectType) Is Null); 

I tried the following solution, but my code did not apply if I chose a value in the Sponsor combo box: https://stackoverflow.com/a/19568169 我尝试了以下解决方案,但是如果我在“赞助者”组合框中选择一个值,我的代码将不适用: https : //stackoverflow.com/a/19568169

How can I develop my code properly? 如何正确开发我的代码?

Thanks. 谢谢。

The idea for this kind of filter is to convert the Null value in the control to SQL that means no conditions on this field. 这种过滤器的想法是将控件中的Null值转换为SQL,这意味着该字段上没有条件。 To do this we use the Nz function and the LIKE keyword. 为此,我们使用Nz函数和LIKE关键字。

The WHERE part should read: WHERE部分应显示为:

WHERE TestInput.[Type of Agreement] LIKE Nz("'" & Forms["Console"].SelectType & "'","'*'") AND
      TestInPut.Sponsor LIKE Nz("'" & Forms["Console"].Sponsor & "'","'*'") AND ...

and so on for each field you want to filter on. 对于要过滤的每个字段,依此类推。 Now, if the combobox is Null, it substitutes LIKE '*' which returns everything for that field. 现在,如果组合框为Null,它将替换为LIKE '*' ,这将返回该字段的所有内容。 If it's populated with a value, it substitutes LIKE 'Grant' for example. 如果使用值填充,则将其替换为LIKE 'Grant' No wildcards (*, ?) mean it's effectively asking for it to be equal. 没有通配符(*,?)表示它实际上要求它相等。

EDIT: Sorry a few errors in that. 编辑:对不起,在那一些错误。 It should be: 它应该是:

WHERE TestInput.[Type of Agreement] LIKE Nz([Forms]![Console]![SelectType],"*") AND
          TestInPut.Sponsor LIKE Nz([Forms]![Console]![Sponsor],"*") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM