0

So the following query statement works just fine. But I need to add an additional field to check, is that possible?

So if FIELD1 or FIELD2 is equal to whatever value is in the Combo254.text, then select those records.

Here is the current query I am running:

Temp = Combo254.Text

Dim strSQL As String
Dim strWhere As String
strWhere = (Chr(34) + Combo254.Text + (Chr(34)))

strSQL = "SELECT * FROM MainQuery WHERE [FIELD1] Like " & strWhere

'MsgBox (strSQL)
[Form_Main].RecordSource = strSQL

1 Answer 1

2

Join each of the additional criteria with the word AND or OR.

As a side note, VBA allows you to include quotation marks in literal strings if you double the quotation marks. I think this is a bit more readable than using Chr(34).

strSQL = "SELECT * FROM MainQuery " & _
    "WHERE [FIELD1] Like """ & Combo254.Text & """ OR " & _
    "[FIELD2] Like """ & Combo254.Text & """"

In this situation, Access doesn't care if you use single quotes or double quotes, so you can rewrite the query like this:

strSQL = "SELECT * FROM MainQuery " & _
    "WHERE [FIELD1] Like '" & Combo254.Text & "' OR " & _
    "[FIELD2] Like '" & Combo254.Text & "'"
Sign up to request clarification or add additional context in comments.

4 Comments

Does this look about right? I'm getting an expected end statement, probably missing a ' or " somewhere. I'm not seeing it.
strSQL = "SELECT * FROM MainQuery " & "WHERE [FIELD1] Like '" & Combo254.Text & "' OR " & "[FIELD2] Like '" & Combo254.Text "'"
You need an & before the final string literal. Combo254.Text & "'". Also, you only need to use & to concatenate strings when you need to join in a variable or split the string over multiple lines. If it's all on one line, then your first & is unnecessary, for example.
Any chance you can edit your post to include it all on one line? My brain is fried right now, so a break will help too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.