Filter based on value in combo box

S

Shael

I have a form with a combo box and a command button. I also have a bunch of
fields on the form that are bound to a query.

I want to be able to select an item in the combo box, click on the command
button and see all the related field in the rest of the form.

Just to experiment, I tried the following code on the button's On Click event:

Private Sub SearchCity_Click()
Me.Filter = "City = New York'"
Me.FilterOn = True
End Sub

This works fine. When I click on the button I see New York record.
I can't seem to find the right syntax to use in the above to use the value
selected in the combo box instead of stating the city explicitly:

Private Sub SearchCity_Click()
Me.Filter = "City = ComboBoxCity.Value"
Me.FilterOn = True
End Sub

Can someone help with the correct syntax? Thanks.
 
F

fredg

I have a form with a combo box and a command button. I also have a bunch of
fields on the form that are bound to a query.

I want to be able to select an item in the combo box, click on the command
button and see all the related field in the rest of the form.

Just to experiment, I tried the following code on the button's On Click event:

Private Sub SearchCity_Click()
Me.Filter = "City = New York'"
Me.FilterOn = True
End Sub

This works fine. When I click on the button I see New York record.
I can't seem to find the right syntax to use in the above to use the value
selected in the combo box instead of stating the city explicitly:

Private Sub SearchCity_Click()
Me.Filter = "City = ComboBoxCity.Value"
Me.FilterOn = True
End Sub

Can someone help with the correct syntax? Thanks.

The value must be concatenated into the string.
And, if the combo box value is a Text value you must surround the
value with single quotes.

Me.Filter = "City = '" & ComboBoxCity & "'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top