There are many ways to create a query in Access. However, creating a query with VBA code is something quite challenging. The code below creates an object of type query, named “MyQuery”. The SQL of the object is also given within the code. The result looks like this:
Thus, we have a new query, which is code-created. Quite interesting way of using the Data Access Objects (DAO) in Access.
Here come the few lines of code:
Dim db As DAO.Database
Dim qDef As DAO.QueryDef
Set db = CurrentDb
Set qDef = db.CreateQueryDef("MyQuery")
qDef.SQL = "SELECT * FROM Vitoshacademy"
Set qDef = Nothing
Set db = Nothing
Enjoy it! 🙂