0

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
  • Since VBA is not just Access (and becuase the solution will possibly not be portable to other uses of VBA), it should be reflected in the question. This is an important limitation, not just a category extension. Commented Oct 13, 2009 at 16:18

3 Answers 3

1

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..
Sign up to request clarification or add additional context in comments.

2 Comments

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: 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.
1

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.

6 Comments

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.
@OtherMicheal there is an access tag.
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.
Hrm. so there is -- completely missed it, since I was focusing on the title and question.
@Larry Lustig: Doesn't DoCmd.RunSQL use ANSI-89 Query Mode? I though "CREATE VIEW" DDL was only supported in ANSI-92 Query Mode.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.