H
Hugh Janus
Hi all,
I am stuck with this (probably simple) problem.
I have a form where I select values from several drop downs to then
pass to a query. This works great. However, now I've added a list
box and I want to pass the values selected from the listbox to filter
further with the query. I want the listbox values to be "OR", i.e.
any of the values selected will be displayed rather than a record that
has all the values. i.e.
The listbox contains:
Bob
John
Susan
Peter
I choose Bob and Peter. So, I want a query which shows the records
containing either Bob or Peter.
I thought I could build a string to then pass to the query but this
does not work. My code looks like this:
Private Sub Supportedon_Click()
Dim ctl As Control
Dim varItem As Variant
Dim strMatch As String
Set ctl = Me.Supportedon
If ctl.ItemsSelected.Count > 0 Then
strMatch = Chr(34) & " OR " & Chr(34) ' = Chr(34) & " " &
Chr(34) & " OR chr(34)" & Chr(34)
For Each varItem In ctl.ItemsSelected
strMatch = strMatch & ctl.ItemData(varItem) & Chr(34) & "
OR " & Chr(34)
Next varItem
If Strings.Left(strMatch, 7) = " " & Chr(34) & " OR " &
Chr(34) Then strMatch = Strings.Right(strMatch, Strings.Len(strMatch)
- 6)
If Strings.Right(strMatch, 6) = Chr(34) & " OR " & Chr(34)
Then strMatch = Strings.Left(strMatch, Strings.Len(strMatch) - 5)
Me.SupportedSTR.Caption = strMatch
End If
End Sub
I am sure that I have plenty of mistakes in the above because I have
spent hours changing it around and stabbing in the dark with it! :-
( I thought I could build a string that said "Bob" OR "Peter" and
then pass it but the query is always empty. If I add "Bob" OR "Peter"
directly into the query via design view it works, thus telling me that
the data is there but the query/form/code is wrong.
Please help!
TIA,
Hugh
I am stuck with this (probably simple) problem.
I have a form where I select values from several drop downs to then
pass to a query. This works great. However, now I've added a list
box and I want to pass the values selected from the listbox to filter
further with the query. I want the listbox values to be "OR", i.e.
any of the values selected will be displayed rather than a record that
has all the values. i.e.
The listbox contains:
Bob
John
Susan
Peter
I choose Bob and Peter. So, I want a query which shows the records
containing either Bob or Peter.
I thought I could build a string to then pass to the query but this
does not work. My code looks like this:
Private Sub Supportedon_Click()
Dim ctl As Control
Dim varItem As Variant
Dim strMatch As String
Set ctl = Me.Supportedon
If ctl.ItemsSelected.Count > 0 Then
strMatch = Chr(34) & " OR " & Chr(34) ' = Chr(34) & " " &
Chr(34) & " OR chr(34)" & Chr(34)
For Each varItem In ctl.ItemsSelected
strMatch = strMatch & ctl.ItemData(varItem) & Chr(34) & "
OR " & Chr(34)
Next varItem
If Strings.Left(strMatch, 7) = " " & Chr(34) & " OR " &
Chr(34) Then strMatch = Strings.Right(strMatch, Strings.Len(strMatch)
- 6)
If Strings.Right(strMatch, 6) = Chr(34) & " OR " & Chr(34)
Then strMatch = Strings.Left(strMatch, Strings.Len(strMatch) - 5)
Me.SupportedSTR.Caption = strMatch
End If
End Sub
I am sure that I have plenty of mistakes in the above because I have
spent hours changing it around and stabbing in the dark with it! :-
( I thought I could build a string that said "Bob" OR "Peter" and
then pass it but the query is always empty. If I add "Bob" OR "Peter"
directly into the query via design view it works, thus telling me that
the data is there but the query/form/code is wrong.
Please help!
TIA,
Hugh