Filter data in a form using a multi select box

G

Guest

Hi there,

Could you please provide me with a code for getting results in a form
details section using a multi select box in the same form (in header) as
filter?

Thanks for a prompt answer
 
G

Guest

Try something like the following untested air-code:

Dim strFilter As String
Dim varSelected As Variant

strFilter = ""
For Each varSelected in Me!MyListBox.ItemsSelected
strFilter = strFilter & Me!MyListBox.ItemData(varSelected) & ", "
Next varSelected

If Len(strFilter) > 0 Then
strFilter = Left$(strFilter, Len(strFilter) - 2)
strFilter = "MyField IN (" & strFilter & ")"
Me.Filter = strFilter
Me.FilterOn = True
End If

(replace MyListBox and MyField with the actual names)

This assumes that MyField is a numeric field. If it's text, you'll need to
add quotes around the values:

strFilter = strFilter & Chr$(34) & _
Me!MyListBox.ItemData(varSelected) & _
Chr$(34) & ", "
 

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