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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Try removing LIKE from keysql.

Does that make any difference?
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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 PrewIT / Software Engineering ConsultantCommented:
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
NorieAnalyst Assistant Commented:
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 PrewIT / Software Engineering ConsultantCommented:
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
NorieAnalyst Assistant Commented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 PrewIT / Software Engineering ConsultantCommented:
Great, glad we sorted that out.  Yes, fine for split for me, it was a collaboration...


»bp
0
mriozzo68Author Commented:
Thanks again everyone.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.