Share via

Why is it not recognizing this as a query?

Maxine Nietz 1 Reputation point
2026-02-04T23:10:10.79+00:00

Why is Access not recognizing this as a query:

Dim lsql As DAO.Recordset

Set lsql = "SELECT OrderHistory.PID, OrderHistory.PONo FROM PONoQ WHERE (((OrderHistory.PONo) Like '*' & [Forms]![POSearchF]![POS] & '*'));"

I get a compile error, type mismatch.

PLEASE HELP!

Thanks in advance.

Microsoft 365 and Office | Access | For education | Windows

1 answer

Sort by: Most helpful
  1. Anonymous
    2026-02-05T01:20:43.3333333+00:00

    Dear @Maxine Nietz,

    Welcome to Microsoft Q&A Forum!

    I reviewed the code you shared, and I wanted to point out the specific mistake in your code so you can correct it easily.

    In the Microsoft DAO example you referenced, the variable strSQL is declared as a String, and rsSQL is declared as a DAO.Recordset. The SQL text is stored in the string variable, and only the result of OpenRecordset is assigned to the recordset variable.

    In your code, however, you used:

    Dim lsql As DAO.Recordset
    Set lsql = "SELECT ... FROM ..."
    

    This causes a Type Mismatch because you're assigning a String ("SELECT ... FROM ...") directly to a Recordset object (DAO.Recordset). The SQL belongs in a String variable (like strSQL), and only the returned Recordset should be assigned to a Recordset variable.

    You can check this article for more details: Database.OpenRecordset method (DAO)

    User's image

    Reference: Type mismatch (Error 13)

    Hope this helps clarify the issue for you. Wish you a good day!


    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.