Create a query programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using AccessXP on Win 2000 with ADO 2.5 and DAO 3.6. Soon will migrate
to Win 2003.

I need to store a query in Access using ADO. How can I do this in code? Or
do I have to use DAO? Either way, can example code please be posted?

Thanks much in advance.
 
Simplest to use the native Access library (DAO):

Dim qdf As DAO.QueryDef
Set qdf = CurrentDb().CreateQueryDef("Query1")
qdf.SQL = "SELECT * FROM Table1;"


If you want to use ADOX, append it to the Views or the Procedures collection
as appropriate:

Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command
cat.ActiveConnection = CurrentProject.Connection
cmd.CommandText = "SELECT * FROM Table1;"
cat.Views.Append "Query1", cmd
 
EXCELLENT! Just what I needed. Thanks for the reply. I prefer to have more
than one way to accomplish something (just in case), so I will file both
methods in my code library.

Thanks much.
 
Back
Top