简体   繁体   English

在MS Access中为子窗体中的下拉框查找创建SQL查询?

[英]Creating an SQL query in MS Access for a dropdown box lookup in a subform?

I have the following two tables, the first called Projects and the second called Parts: 我有以下两个表,第一个叫做Projects,第二个叫做Parts:

在此输入图像描述

在此输入图像描述

I then have a form (lets call it Form 1) that uses Projects as its Record Source with a subform that links Project on the Projects table to Project on the Parts table and displays only the items associated with the selected Projects record source, like so: 然后,我有一个表单(我们称之为表单1),它使用Projects作为其Record Source,其子表单将Projects表上的Project链接到Parts表上的Project,并仅显示与所选Projects记录源关联的项,如此:

在此输入图像描述

Now, what I'd like to be able to do is have a dropdown on Form 1 that only has the Items listed on the subform selectable, but I can't seem to find the SQL code to do this. 现在,我希望能够做的是在表单1上有一个下拉列表,只有子表单上列出的项目可选,但我似乎无法找到执行此操作的SQL代码。

My current dropdown uses the following code, but of course this just shows all items, not the ones only on the subform: 我当前的下拉列表使用以下代码,但当然这只显示所有项目,而不是仅显示在子表单上的项目:

SELECT [Parts].[ID], [Parts].[Item] FROM Parts ORDER BY [Item]; 

What I'd like to do would be like this I think, but obviously using the correct syntax: 我想要做的就是这样,但显然使用了正确的语法:

SELECT [Parts].[ID], [Parts].[Item] WHERE [Parts].[ID]= & Me![ID]  FROM Parts  ORDER BY [Item]; 

Put this in the form's Load event: 把它放在表单的Load事件中:

Me!MyCombo.RowSource = "SELECT [Parts].[ID], [Parts].[Item] FROM Parts WHERE [Parts].[ID]= '" & Me![ID] & "' ORDER BY [Item];"
Me!MyCombo.Refresh

You will need to take the single quotes out of it if Parts.ID is a Numeric field, and leave them in if it's a Text field. 如果Parts.ID是数字字段,则需要从中取出单引号,如果它是文本字段,则将它们保留。

Use the form's "Current" event to set the combo's RowSource property, so whenever the active row in your form changes you get the updated list on your combo. 使用表单的“当前”事件来设置组合的RowSource属性,因此每当表单中的活动行发生更改时,您将获得组合中的更新列表。

Me!MyCombo.RowSource = "SELECT Project, Item FROM Parts WHERE Project = '" & Me.Project & "' ORDER BY Item"

Sorry, user2174085: This should be a comment on you answer, but I don't have the option to make comments available. 对不起,user2174085:这应该是您回答的评论,但我没有选项可以提供评论。

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

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