As you found, this is not going to work, as the operators you type into the
text box will be treated as a value (not operators).
Where is this query going? You can probably build the SQL string (or WHERE
clause) from the values instead of placing them in the query.
For example, if this is to filter a report, you could leave the criteria out
of the query, and build the WhereCondition for OpenReport from the values in
the text box. If it is to filter a form, you could build the same string,
and assign it to the Filter property of the form (remembering to set
FilterOn as well.) Or, if you build the entire query statement, you could
assign it to the SQL property of a QueryDef, or the RecordSource property of
a form, or the RecordSource of a report (in the report's Open Event), or use
it to OpenRecordset() in code.
If the user is choosing from existing values, you could also use a
multi-select list box, as explained here:
Use a multi-select list box to filter a report
at:
http://allenbrowne.com/ser-50.html
Of if you just want the user to be able to type a bunch of keywords into a
text box, and then show any record that has any of these words in (say) the
Notes field, this example shows how to set the form's Filter in the
AfterUpdate event of the text box where the user enters the words:
Private Sub txtKeywords_AfterUpdate()
Dim strWhere As String
Dim strWord As String
Dim varKeywords As Variant 'Array of keywords.
Dim i As Integer
Dim lngLen As Long
If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If IsNull(Me.txtKeywords) Then 'Show all if blank.
If Me.FilterOn Then
Me.FilterOn = False
End If
Else
varKeywords = Split(Me.txtKeywords, " ")
If UBound(varKeywords) >= 99 Then '99 words max.
MsgBox "Too many words."
Else
'Build up the Where string from the array.
For i = LBound(varKeywords) To UBound(varKeywords)
strWord = Trim$(varKeywords(i))
If strWord <> vbNullString Then
strWhere = strWhere & "([Notes] Like ""*" & strWord &
"*"") OR "
End If
Next
lngLen = Len(strWhere) - 4 'Without trailing " OR ".
If lngLen > 0 Then
Me.Filter = Left(strWhere, lngLen)
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End If
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Senexis said:
I'm trying to make a form with a text box for the user to be able to type
in
a list of numbers (like "0001" or "0003" or "0005" ) so that these numbers
can be used in a query later on to return those records (with details
0001,
0003, and 0005).
I've been trying to do this with this in the criteria
Like "*" & [Forms]![frm_B_LOAD]![detail_box] & "*" Or Is Null
[detail_box] = "0001" or "0003" or "0005"
However for some reason it's not taking. What am I doing wrong? I
believe
I can successful do a ranged query based off of two given numbers from the
user, but I'm having trouble letting the user provide specific numbers.
Thanks in advance!