Using wildcards with a LIKE statement

This is a continuation of a previous question (http://bit.ly/2yRemJp).

I have a textbox (key_sel) on a form (switchboard) that I want to allow the user to enter in a series of values separated by a semicolon, which will then be used as criteria in a query.

SQL statement is below:
 
keysql = "SELECT dbo_sys1_key_codes.key_code FROM dbo_sys1_key_codes Where key_code LIKE ('" & Replace(Trim(Forms!Switchboard!key_sel), ";", "' or '") & "')"

If I use one value (*2XH07) the query runs fine and pulls the correct data.  When I specify multiple criteria (I used *2XH07;*9XH16 as the value) it pulls no data.  When I design view the query the query the criteria is below.  Obviously the first "Like " is creating the problem.  

Like (Like '*2XH07' Or Like '*9XH16')

Don't know what I'm doing wrong.
mriozzo68Asked:
Who is Participating?
 
Bill PrewCommented:
Good point Norie, true.  So maybe:

keysql = "SELECT dbo_sys1_key_codes.key_code FROM dbo_sys1_key_codes Where key_code LIKE ('" & Replace(Trim(Forms!Switchboard!key_sel), ";", "') OR key_code LIKE ('") & "')"

Open in new window


»bp
1
 
NorieVBA ExpertCommented:
Try removing LIKE from keysql.

Does that make any difference?
0
 
Bill PrewCommented:
Will there always only be 2 values entered, or could it be any number?

You probably need to SPLIT() the input value at ";", and then loop over the array adding OR LIKE's to the WHERE clause to get to a final query.


»bp
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bill PrewCommented:
How about this?

keysql = "SELECT dbo_sys1_key_codes.key_code FROM dbo_sys1_key_codes Where key_code 
LIKE ('" & Replace(Trim(Forms!Switchboard!key_sel), ";", "') OR LIKE ('") & "')"

Open in new window


»bp
0
 
mriozzo68Author Commented:
@Norie - access throws an undefined function error if LIKE is not there
@Bill Prew - it can be any number of values.  Not sure I understand, the "Or Like"s get added in when the query runs.
This is how the query that runs translates the criteria statement -  Like (Like '*2XH07' Or Like '*9XH16')
0
 
Bill PrewCommented:
Sorry, typo.

keysql = "SELECT dbo_sys1_key_codes.key_code FROM dbo_sys1_key_codes Where key_code LIKE ('" & Replace(Trim(Forms!Switchboard!key_sel), ";", "') OR LIKE ('") & "')"

Open in new window


»bp
0
 
mriozzo68Author Commented:
@Bill Prew - both statements throw a syntax error, saying there is a missing operator.  The criteria is not being parsed correctly.  It's translating as
key_code LIKE ('*2XH07') OR LIKE ('*PXH16')
0
 
NorieVBA ExpertCommented:
I think you need to use, as Bill suggested, Split to get an array of the values entered in the textbox.

You could then use that array to create the criteria for your WHERE clause.
0
 
Bill PrewCommented:
What and where is "dbo_sys1_key_codes" ?  That sounds like a SQL Server table name rather than an Access one.  Could you be doing a passthrough query or something?  A little more context might help...


»bp
0
 
mriozzo68Author Commented:
I guess I'm missing the point here.  From what I can see, the criteria statement is generating correctly it's just that Access is formatting the LIKE statement.  It's the initial Like( that is causing the problem.  The "or like" operators get built in correctly.
0
 
NorieVBA ExpertCommented:
I think you might need to repeat the field name for each value.

Something like this:

WHERE fieldname Like 'val1*' OR fieldname Like 'val2*' OR fieldname Like 'val3*' ...
1
 
mriozzo68Author Commented:
the dbo table is an ODBC linked SQL table.  We use Access as the front end tool.  I have a switchboard form that users use to run preset queries.  I was trying to simplify/expand the way that the users can enter criteria.  Right now, the form is setup, rather inelegantly, with a number of text boxes that the users can enter one value into each, with or without wildcards.  It runs fine, but they are limited to six variables.  I could, I supposed just add a bunch more text boxes to make more choices available.

I was just trying to streamline by giving them one text field to enter their variables in separated by semicolons, then using the above SQL statements to parse the stream and build the query.  If you look at the question I linked, I originally got a solution which used an IN statement to create the query.  That worked fine for one of the other reports because the data being used was specific.  Now I decided to try to change over another set of queries that use data that needs to be wildcarded and I'm stuck again.

I really tried many permutations of the SQL statement prior to reposting, but nothing is working.
0
 
mriozzo68Author Commented:
For further context, the command button I'm using is setup like this to fire the query

Private Sub Command275_Click()

Dim Query As DAO.QueryDef

Dim keysql As String

keysql = "SELECT dbo_sys1_key_codes.key_code FROM dbo_sys1_key_codes Where key_code LIKE ('" & Replace(Trim(Forms!Switchboard!key_sel), ";", "' or '") & "')"

Set Query = CurrentDb.QueryDefs("qry_mix_key_select")

Query.Sql = keysql

DoCmd.OpenQuery ("qry_mix_key_select")

End Sub
0
 
mriozzo68Author Commented:
OK @Bill Prew and @Norie, that did it.  Gosh I wish I was as smart as you people.  I'd get more done a lot faster.  OK splitting points?
0
 
Bill PrewCommented:
Great, glad we sorted that out.  Yes, fine for split for me, it was a collaboration...


»bp
0
 
mriozzo68Author Commented:
Thanks again everyone.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.