Skip to content
Advertisement

Excel VBA with SQL : No row returned when Where clause specified

Here below an excerpt of my codes.

This is the SQL command :

Where the database located :

The variable containing the string connection to the DB :

The wrkSheet for the result :

This command Adding QueryTable as a ListObject returns ZERO occurrence because of Where Clause whereas the SQL is OK.

If the Where Clause is removed, it works but I need to add restrictions on Rows.

What’s wrong ? I absolutely need to use Where clause.

Advertisement

Answer

The LIKE wildcard behaves differently when running queries between the MS Access GUI (frontend) and any ODBC/OLDEB connection to MS Access (backend). See differences between ANSI-89 and ANSI-92 in MSDN docs.

For ODBC/OLEDB connections as you are doing in Excel, LIKE requires the ANSI-92 wildcard with %:

Alternatively, to be compatiable in both use ALIKE (ANSI-Like) in GUI and ODBC/OLEDB:


Even better, save the query in MS Access (which is more efficient as the engine caches stats and the best execution plan):

Then, reference its name in Excel (avoiding VBA concatenation):

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement