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.
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.
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*”).
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.
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 ('*')
TOP [number]
is arbitrary without an ORDER BY
clauseLike
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 '%'
In ADO/VBA, you have to use % instead of * as the wildcard. I ran into this a couple times in the past ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With