简体   繁体   中英

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:

在此输入图像描述

在此输入图像描述

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:

在此输入图像描述

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.

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:

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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