OK back to square 1.
What do counties have to do with your issue?
You would not use like when you are dealing with County names.
So your sample code with the counties is just to make some sql based on a multi select listbox?
Please state your issue again, based on the code you found, and tell exactly
what will be in the listbox?
will it be multiselect?
what field(s) will you be comparing/looking for Like X?
=========================end of original ================================
A new look at your material (since you were offline and I wasn't likely to get answers before doing some tests)
I looked at your code again and revised the procedure to the following in order to get a Like clause.
Note, I am assuming you will use a multi select listbox --- using an All, and a number of strings
I am building SQL that will find records
Where somefield Like '*string1*' or somefield Like '*string2*'
note: string1 and string2 from your listbox selections.
There are commented debug.print statements that you can activate by removing the leading ' character
Most of my comments have a '..orange within the comment
NOTE: You can try this code in your database, but you should FIRST rename your procedure so it is not overwritten and lost/Gone/Deleted
Code:
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim YourFld As String
YourFld = "TheFieldToLookIn " '.. orange
Dim flgSelectAll As Boolean
Dim varItem As Variant
' Debug.Print "You selected " & vbCrLf & vbCrLf '..orange
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblCompanies"
'Build the IN string by looping through the listbox
For i = 0 To lstCounties.ListCount - 1
Debug.Print "i " & i & " " & IIf(lstCounties.Selected(i), "TRUE Selected " & lstCounties.Column(0, i), "FALSE ") 'orange..
If lstCounties.Selected(i) Then
If lstCounties.Column(0, i) = "All" Then
'if you Select All then No WHERE clause is needed ..orange
strWhere = "" 'make it 0 length string ..orange
flgSelectAll = True
Exit For
End If
'strIN = strIN & "'" & lstCounties.Column(0, i) & "',"
' Debug.Print "strIn " & strIN '..orange
'New Code ---------------we know some thing was selected and it wasn't All
'=========
strIN = strIN & " (" & YourFld & "LIKE '*" & lstCounties.Column(0, i) & "*') OR "
End If
Next i
'=== This block commented ..orange
' 'Create the WHERE string, and strip off the last comma of the IN string
' strWhere = " WHERE [strCompanyCounty] in (" & Left(strIN, Len(strIN) - 1) & ")"
'====
' 'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
'remove the last OR from strIn
strWhere = " WHERE "
strIN = Mid(strIN, 1, Len(strIN) - 4)
strSQL = strSQL & strWhere & strIN
Debug.Print strSQL
End If
' NOTE *** This query won't work because I have a field that makes no sense/not in the table
' MyDB.QueryDefs.Delete "qryCompanyCounties"
' Set qdef = MyDB.CreateQueryDef("qryCompanyCounties", strSQL)
'Open the query, built using the IN clause to set the criteria
'' DoCmd.OpenQuery "qryCompanyCounties", acViewNormal
'How many items were selected ..orange
Debug.Print " # selected items " & Me.lstCounties.ItemsSelected.Count '..orange
'Clear listbox selections after running query
For Each varItem In Me.lstCounties.ItemsSelected
Me.lstCounties.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
This code outputs sql for a query as follows: (This was for testing using your listbox and my choice of selections. Also if you choose ALL, no Where clause is added to the SQL.
SELECT * FROM tblCompanies WHERE (TheFieldToLookIn LIKE '*Dorset*') OR (TheFieldToLookIn LIKE '*Lincolnshire*') OR (TheFieldToLookIn LIKE '*Nottinghamshire*')
Post back with questions before you replace your proc code with the code given here.