Solved

Why can't I use the Like Predicate

Posted on 1998-07-22
8
273 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
 
LVL 1

Expert Comment

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

The quotes have to be Outside.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now