添加“全部”用于在 MS Access 中过滤报告的组合框选项

发布于 2024-09-30 11:38:08 字数 541 浏览 2 评论 0原文

我正在尝试按照 Microsoft 的示例了解如何将“全部”选项添加到组合框Microsoft Access,但除了指定代码之外,他们的文章没有提供足够的指导。

我想做的是构建一个表单,允许用户从组合框中选择一个选项(选项是从表中的记录生成的),然后构建一个根据用户选择的选项进行过滤的报告。组合框由 2 列组成:记录的主键/ID 及其可显示的名称。

我无法理解 Microsoft 提供的 VBA 代码足以弄清楚发生了什么,但我希望组合框中的“全部”选项要么有一个空白主键/ID,要么有一个 = 0。那不是在这种情况下,使用表单时选择“全部”选项会导致错误消息“您输入的值对此字段无效”。这让我相信“全部”文本被填充到主键/ID 列而不是显示列中。该示例指示我将显示列号分配为 ComboBox 的“Tag”属性 - 在本例中,我的显示列号为 2。但是,这(以及我添加的几乎任何其他值)会导致上述错误信息。

知道微软的例子是否适用于我的情况,或者我是否需要以某种方式调整他们的代码?

I'm trying to follow Microsoft's example on how to add an "All" option to a ComboBox in Microsoft Access, but their article does not do an adequate job of providing guidance, aside from specifying the code.

What I'm trying to do is build a form that allows a user to select an option from a ComboBox (the options are generated from records in a table), and then build a report filtered based on the user's selected option. The ComboBox consists of 2 columns: the primary key/ID of the records and their displayable names.

I can't understand the VBA code Microsoft provides enough to figure out what is going on, but I would like the "All" option in my ComboBox to either have a blank primary key/ID, or one that = 0. That isn't the case, as selecting the "All" option when using the form results in the error message "The value you entered isn't valid for this field". This leads me to believe that the "All" text is getting filled into the primary key/ID column instead of the display column. The example instructs me to assign the display column number as the "Tag" property of the ComboBox - and in this case, my display column number is 2. However, this (and pretty much any other value I add) results in the aforementioned error message.

Any idea if Microsoft's example is even applicable to my case, or do I need to adjust their code somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-10-07 11:38:08

检查组合框的“控制源”属性。听起来它可能绑定到表单记录源中的字段。如果您将其设置为未绑定控件(控件源属性中没有任何内容),您应该能够从组合的行源中选择任何项目,而 Access 不会向您抱怨。

假设您的组合的行源是这样的查询:

SELECT id, disp_name
FROM YourTable
ORDER BY disp_name;

您可以使用 UNION 查询添加“所有”行:

SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT TOP 1 0, "**ALL**"
FROM AnyTable
ORDER BY disp_name;

AnyTable 可以就是这样。如果您碰巧有一个只包含一行的表,请使用该行...您甚至不需要 TOP 1 部分。只是尽量不要使用某些ReallyBigTable 作为AnyTable。

编辑:实际上,如果某些ReallyBigTable具有主键或其他唯一字段,您可以在WHERE子句中使用这些字段来检索单个行,那么它就可以了:

SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT 0, "**ALL**"
FROM ReallyBigTable
WHERE pk_field = 1
ORDER BY disp_name;

UNION ALL将返回所有组合行。如果有任何重复的行,您可以仅使用 UNION 而不是 UNION ALL 来减少它们。

Check the Control Source property of your combo box. Sounds like it may be bound to a field in the form's record source. If you make it an unbound control (nothing in the Control Source property) you should be able to select any item from the combo's Row Source without Access complaining at you.

Say your combo's Row Source is a query like this:

SELECT id, disp_name
FROM YourTable
ORDER BY disp_name;

You can add an "all" row with a UNION query:

SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT TOP 1 0, "**ALL**"
FROM AnyTable
ORDER BY disp_name;

AnyTable can be just that. If you happen to have a table which contains only a single row, use that one ... and you wouldn't even need the TOP 1 part. Just try not to use some ReallyBigTable as AnyTable.

Edit: Actually some ReallyBigTable would be fine if it has a primary key or other unique field which you can use in a WHERE clause to retrieve a single row:

SELECT id, disp_name
FROM YourTable
UNION ALL
SELECT 0, "**ALL**"
FROM ReallyBigTable
WHERE pk_field = 1
ORDER BY disp_name;

UNION ALL will return all combined rows. If you have any duplicate rows, you can thin them out by using just UNION instead of UNION ALL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文