Link to home
Create AccountLog in
Avatar of mriozzo68
mriozzo68Flag for United States of America

asked on

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.
Avatar of Norie
Norie

Try removing LIKE from keysql.

Does that make any difference?
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
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
Avatar of mriozzo68

ASKER

@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')
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
@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')
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.
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
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.
SOLUTION
Avatar of Norie
Norie

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
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?
Great, glad we sorted that out.  Yes, fine for split for me, it was a collaboration...


»bp
Thanks again everyone.