Using wildcards with a LIKE statement

mriozzo68
mriozzo68 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Try removing LIKE from keysql.

Does that make any difference?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

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')
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Author

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')
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.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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

Author

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.
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*' ...

Author

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.
Test your restores, not your backups...
Top Expert 2016
Commented:
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

Author

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

Author

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?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Great, glad we sorted that out.  Yes, fine for split for me, it was a collaboration...


»bp

Author

Commented:
Thanks again everyone.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial