Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Like * Works in MS-Access but Not VBA

I have a simple query but am running into problems using LIKE in VBA. My SQL string in VBA is:

stsql1 = "Select Top 25 data.* from data where data.Description Like ('*') "

When I run this sql string in my VBA code I get no records returned, but if I copy/paste the same string into a query in SQL View in MS Access, the query returns the values I expect. Is there a trick to using the "Like" syntax in VBA?

I can provide additional code and a small version of the database if that would help.

like image 673
Rich Estabrook Avatar asked May 17 '11 21:05

Rich Estabrook


People also ask

How do you use like in Access VBA?

In an expression, you can use the Like operator to compare a field value to a string expression. For example, if you enter Like "C*" in an SQL query, the query returns all field values beginning with the letter C. In a parameter query, you can prompt the user for a pattern to search for.

What is like used for in access?

Remarks. You can use the Like operator to find values in a field that match the pattern you specify. For pattern, you can specify the complete value (for example, Like “Smith”), or you can use wildcard characters to find a range of values (for example, Like “Sm*”).

Is Microsoft Access like SQL?

Microsoft Access and Microsoft SQL Server are both database applications. The major difference between the two is in how the software is used. Microsoft Access is used in small business applications. Microsoft Access is also unable to handle large quantities of database queries.


2 Answers

For SQL, the database engine will accept either single or double quotes as text delimiters. So either of these 2 WHERE clauses will work.

WHERE some_field Like '*'
WHERE some_field Like "*"

VBA however only accepts double quotes as text delimiters, so you would have to use the second form.

Two other points about your SELECT statement:

Select Top 25 data.* from data where data.Description Like ('*')
  1. TOP [number] is arbitrary without an ORDER BY clause
  2. You don't need parentheses surrounding your Like pattern ... you can use Like "*"

If your VBA code is using ADO with that SELECT statement, you must change the wild card character from * to % ...

WHERE data.Description Like '%'
like image 123
HansUp Avatar answered Oct 01 '22 20:10

HansUp


In ADO/VBA, you have to use % instead of * as the wildcard. I ran into this a couple times in the past ....

like image 45
Jacob Avatar answered Oct 01 '22 18:10

Jacob