Solved

Why can't I use the Like Predicate

Posted on 1998-07-22
8
275 Views
Last Modified: 2010-05-03
I'm using ADO to connect to an Access 95 database through a DSN.  The problem is I have SQL statement similar to the following

SELECT * FROM [Table] WHERE [FieldName] Like('*" & FieldValue & "*') ORDER BY [FieldName2]

Now there is nothing wrong with the SQL string because if I go into Access and put this as a query it returns the correct records.  The problem is when I run the following line:

  rs.Open sSQL, "DSN=Town;uid=;pwd=", adOpenForwardOnly, adLockReadOnly

I get this error:

Expression cannot be used with the Like predicate. in query expression '[Owner]  alike('*Name*')'.
0
Comment
Question by:PCav
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:BeerMan
ID: 1466400
Try this, i think it's gonna work !

dim query as string
query = "Mr. Jones"   ' Exemple !
query = "*" & query & "*"
"SELECT * From [Table] " & "WHERE [Field] LIKE " & "'" & query & "' "

0
 

Author Comment

by:PCav
ID: 1466401
I no longer get an error, but it doesn't return any records even though there are some that me the criteria.
0
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1466402
Use the same answer as BeerMan gave, but include the asterisks inside the quotes. There's no asterisks in there.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Expert Comment

by:BeerMan
ID: 1466403
"SELECT * From [Table] " & "WHERE [Field] LIKE " & " ' " & query & " '

The quotes have to be Outside.

0
 

Author Comment

by:PCav
ID: 1466404
They are inside the quotes if you look it say 'query' where query = *Mr.Jones*.
0
 

Author Comment

by:PCav
ID: 1466405
This is what my final SQL Statement evalutes to

SELECT [ID],[Location],[Owner],[Map],[Lot],[Unit] FROM [Town] WHERE  [Owner]  Like '*diesso*' ORDER BY [Owner]

the odd thing is if I run this exact same query in Access it returns one record (which is what it is supposed to do), but doing

rs.open ABOVESQLSTATEMENT, cn, adOpenForwardOnly, adLockReadOnly

where rs is an ADODB.Recordset object and cn is a ADODB.Connection object I get rs.eof evaluting to TRUE.  Now I know the connection to the database is valid because I can run a different SQL Query w/out the like predicate and it returns the way it is suppose to.
0
 
LVL 1

Accepted Solution

by:
wizard2072098 earned 50 total points
ID: 1466406
I know that when you run a LIKE predicate in Transact-SQL on SQL Server, you have to use the "%" instead of "*" as a wildcard character. Maybe you should try changing your asterisks to percent signs and see if it returns something.
0
 

Author Comment

by:PCav
ID: 1466407
Thanks that works.

0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question