如何在 Access VBA 表单中创建一个 'All' 选项作为查询参数传递?

How to create an 'All' option in Access VBA forms to be passed as a query parameter?

我有一个 table 命名的人,例如:

     ID        Name

     1         John
     2         Jerry
     3         Jack

我已经设置了一个名为 Form1 的表单和一个名为 Combo1 的组合框。

Combo1 有 3 个值-

    1
    2
    3
    All

然后我设置了以下查询:

    Select * From Persons WHERE Persons.ID = (Forms![Form1]![Combo1]);

当 1,2 或 3 在组合框中 select 时,查询工作正常,但我无法弄清楚如何使它对 select 所有记录起作用,当用户selects 'All'。

我不明白为什么需要 VBA。在我看来,您只需查询即可获得所需的内容...

Select *
From Persons
WHERE
        Persons.ID = Forms![Form1]![Combo1]
    OR Forms![Form1]![Combo1] = 'All';

当在组合框中选择 All 时,table 中所有行的条件 Forms![Form1]![Combo1] = 'All' 都将为真。因此 none 被排除在查询的结果集中。

当在组合框中选择 All 以外的任何内容时,查询将 return 仅 ID 值与组合框值匹配的行。

不确定您的 Combo1 现在是什么样子? 您是否要求显示 ID > 1 的所有条目或所有条目 > 比组合 1 中选择的 ID - 组合 1 中是否还有 ALL 选项?

如果您只是问最后一个问题:

How can I make it work so that the user can view All Names for ID's greater than 1?

那你大概可以用

Select * From Persons WHERE Persons.ID > 1 
AND (Persons.Name = Forms![Form1]![Combo2] OR Forms![Form1]![Combo2] = 'All');

但现在我认为你让每个人都对你想要的东西感到困惑。希望我猜对了,让你走上正确的道路