views:

232

answers:

3

If I want to code the following in VBA how do I do it

QUERY1:
SELECT field1, Min(field4) AS MinField4, Max(field5) AS MaxField5
FROM Table1
GROUP BY field1;


SELECT Query1.field1, Table1.field2, Table1.field3, Query1.MinField4,
       Query1.MaxField5
FROM   Query1 INNER JOIN Table1 ON (Query1.field1 = Table1.field1) AND
       (Query1.MinField4 = Table1.field4) AND
       (Query1.MaxField5 = Table1.field5);

I know for executing the SQL I store it as as string and write run SQL. but how do I code storing query1 as a persistent object that can be referenced in other SQL statements?

+1  A: 

I take it your question is "How do I create a new query in Microsoft Access using code?"

There are two solutions:

  1. Microsoft Access will accept the DDL statement CREATE VIEW. So you can construct that statement in code and then execute it against the database via OLE DB e.g. ADO in VBA code.
  2. The database object in MS Access contains a collections property called QueryDefs and you can access that to manipulate (and create) queries stored in the database.
Larry Lustig
I don't see where the poster wants to use Access; VBA is also used as a scripting language for 3rd-party applications, like FormWare or InputAccel.
Michael Paulukonis
@OtherMicheal there is an access tag.
John Nolan
Uhh. . . Seeing as I found the question by clicking on the Access tag-of-interest, and seeing as how the user tagged his or her question "Access", and seeing as how the question references the RunSQL method of the DoCmd object, I thought (and still think) it is reasonable to assume that he or she is using Access.
Larry Lustig
Hrm. so there is -- completely missed it, since I was focusing on the title and question.
Michael Paulukonis
@Larry Lustig: Doesn't DoCmd.RunSQL use ANSI-89 Query Mode? I though "CREATE VIEW" DDL was only supported in ANSI-92 Query Mode.
onedaywhen
@onedaywhen — You are correct. CREATE VIEW must be submitted via ADO. Will edit my response to reflect that.
Larry Lustig
A: 

you could just create a query and paste that SQL into the SQL View (available from the query design window).

Nick
+1  A: 

Here some code that will fill up a dataset with your results

Dim cnn As ADODB.Connection
Dim recordset As ADODB.Recordset
Dim strSQL As String

Set cnn = CurrentProject.Connection
strSQL = "SELECT blah ..."
recordset.Open strSQL, cnn, adOpenKeyset, adLockOptimistic, adCmdText
'do what you want now.

oh yeah its been a while but you probably want to clean up too

Set recordset = Nothing
'etc..
John Nolan
In Access/VBA, it's not necessary to cleanup after ADO (though it's certainly good housekeeping to at least close your recordset). It's only DAO that had the reference counting problems, and it seems to me from what I've read that they are substantially lessened these days in comparison to the A97 time frame.
David-W-Fenton
@David W. Fenton: Could be a good idea to call the Connection's Close method at the earliest opportunity when done but hard to generalize really. Also note that you sometimes need to set an ADO Catalog to Nothing in order to close the connection and release from connection pooling etc.
onedaywhen