Solved

Why can't I use the Like Predicate

Posted on 1998-07-22
8
274 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now