VBA – Create Query with VBA
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:
Sub CreateQuery()
Dim db As DAO.Database
Dim qDef As DAO.QueryDef
Set db = CurrentDb
Set qDef = db.CreateQueryDef("MyQuery")
qDef.SQL = "SELECT * FROM Vitoshacademy"
qDef.Close
db.Close
Set qDef = Nothing
Set db = Nothing
Application.RefreshDatabaseWindow
End Sub
Enjoy it! 🙂