Skip to content
Advertisement

Excel VBA SQL from Access no results

Using Excel 2010 to query an Access 2010 Database (via UserForms).

When I execute the code, I get my Message Box “No Results” (called near the end of the sub). However, there should be 12 records that pull up when I enter in a certain search string.

I thought maybe my SQL string was incorrect so I wrote the SQL statement to Sheet1 Cell A2. I then opened up my Access Database, created a SQL query, and copy/pasted the SQL statement from cell A2 – It worked perfectly. –> So it’s not the SQL statement.

Why is my code not finding the data? The SQL statement works fine. I’m not getting any errors when I try to establish an ADODB connection.

EDIT: I use the exact same database connection setup in another sub and it works fine.

Advertisement

Answer

I think I know why this is happening. The wildcard in Access is *, but for most other variants of SQL it is %. You are using ADO here. See this

Try this instead:

keyStr = "%" & searchStr & "%" 'Not sure if you need the extra "'s either

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