Andy
Andy

Reputation: 135

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:

enter image description here

enter image description here

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:

enter image description here

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]; 

Upvotes: 0

Views: 2712

Answers (2)

Clon
Clon

Reputation: 1055

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.

Upvotes: 1

Johnny Bones
Johnny Bones

Reputation: 8402

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.

Upvotes: 1

Related Questions