Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Why can't I use the Like Predicate

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
PCav
Asked:
PCav
  • 4
  • 2
  • 2
1 Solution
 
BeerManCommented:
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
 
PCavAuthor Commented:
I no longer get an error, but it doesn't return any records even though there are some that me the criteria.
0
 
wizard2072098Commented:
Use the same answer as BeerMan gave, but include the asterisks inside the quotes. There's no asterisks in there.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
BeerManCommented:
"SELECT * From [Table] " & "WHERE [Field] LIKE " & " ' " & query & " '

The quotes have to be Outside.

0
 
PCavAuthor Commented:
They are inside the quotes if you look it say 'query' where query = *Mr.Jones*.
0
 
PCavAuthor Commented:
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
 
wizard2072098Commented:
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
 
PCavAuthor Commented:
Thanks that works.

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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now