MS Access 根据表单值传递
如何将传递查询分配给依赖于表单中另一个值的行源?
本质上我想这样做:
SELECT x.companyid,
x.companyname,
x.productid
FROM x
WHERE (((x.CompanyID) = [Forms]![Reporting]![CompanyID_Control]))
ORDER BY x.productid;
但是传递查询当然不支持对任何表单控件的引用。
How do I assign a pass-through query to Row Source that is dependent on another value in the form?
Essentially I want to do this:
SELECT x.companyid,
x.companyname,
x.productid
FROM x
WHERE (((x.CompanyID) = [Forms]![Reporting]![CompanyID_Control]))
ORDER BY x.productid;
But of course pass-through queries do not support reference to any form controls.
I have read here that there is a method via VBA, however I do not know how to use VBA in conjunction with the Row Source of a control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Remou 在他的回答中所说,链接表将使这变得更容易。但是,如果您有名为
MyQuery
的传递查询,则可以执行以下操作,以使MyComboOrListBox
控件的 RowSource 在的值变化时动态更新。 CompanyID_Control
更改:您还需要将
CompanyID_Control
的AfterUpdate
属性设置为:[事件过程]
。请注意,即使您按照 Remou 建议使用链接表,您仍然需要在
CompanyID_Control
的AfterUpdate
事件中编写代码来刷新组合框/列表框 RowSource:As Remou stated in his answer, linked tables will make this easier. However, if you have a pass-through query named
MyQuery
, you can do the following to make the RowSource of aMyComboOrListBox
control update dynamically when the value of theCompanyID_Control
changes:You'll also need to set the
AfterUpdate
property of theCompanyID_Control
to:[Event Procedure]
.Note that even if you use linked tables as Remou suggested, you will still need code in the
AfterUpdate
event of theCompanyID_Control
to refresh your combobox/listbox RowSource:在某些情况下,通过使用直通查询作为 FROM 源编写 SQL SELECT,您将获得高效的结果:
这很可能与在代码中编辑直通的 QueryDef 一样高效,并且仅当直通不成功时才会失败。 t 在其自己的 SELECT 中返回您需要过滤的字段。是否值得更改取决于您使用直通的目的以及它的复杂程度。
一般来说,我总是避免编辑保存的 QueryDef。考虑一下:您多久会使用 DDL 更改 SQL Server VIEW?不经常!在 Access 中,它可能会导致轻微的膨胀(或者在某些情况下不那么轻微),并且我总是尽可能避免任何导致我的前端膨胀的事情。
In some cases, you will get efficient results by writing a SQL SELECT using your passthrough query as the FROM source:
This will very likely be just as efficient as editing the passthrough's QueryDef in code, and it's going to fail only when the passthrough doesn't return in its own SELECT the fields you need to filter on. Whether or not it's worth changing that depends on what you're using the passthrough for, and how complicated it is.
In general, I always avoid editing saved QueryDefs. Consider this: how often would you alter a SQL Server VIEW with DDL? Not often! In Access, it can lead to minor bloat (or not-so-minor, in some cases), and I always avoid anything that causes my front end to bloat, insofar as that's possible.
如果表已链接,您只需对其运行查询,就像它是 Access 表一样,这包括引用表单。所以:
会很好地工作。
要永久更改查询的 SQL,可以使用 QueryDef 的 SQL 属性:
您还可以将表单记录源或组合或列表框的行源设置为 SQL 字符串,使用链接表就足够简单了:
或者
If a table is linked, you can just run a query against it as if it was an Access table, this includes referring to forms. So:
Will work fine.
To permanently change the SQL of a query, you can use the SQL property of the QueryDef:
You can also set a forms Record Source or the row source of a combo or listbox to an SQL string, with a linked table it is simple enough:
Or
经过大量尝试后我才发现,这是一个简单的解决方案,用您想要在表单中调用的内容创建直通,但将其留空,以便它调用所有内容,然后保存直通。关闭该查询并创建一个新查询并添加 passthrouh 中的每一列。现在,在调用 passthrough 的新查询的条件中添加
[Forms]![Reporting]![CompanyID_Control]
) 和只需确保表单已打开,应该运行得同样快,但现在您可以使用表单I just figured it out after tons of trying, this is an easy fix, create your passthrough with what ever you want to call in the form but leave it blank so it calls everything and then save the passthrough. Close that out and create a new query and add every column from the passthrouh in. Now in the criteria of the new query thats calling the passthrough add
[Forms]![Reporting]![CompanyID_Control]
) and just make sure the form is open, should run just as fast but now you can use your forms