Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving a Query 1

Status
Not open for further replies.

irishjoe

Programmer
Aug 5, 2002
76
AU
Does anyone know if it is possible to create and save a query using VBA?

I would like to be able to create something like the following...
Code:
"Select * from tblUsers Where [location]='Australia'"
and save the query as qryAussieUsers

Is this possible?
 
How are ya irishjoe . . .

have a look at [blue]Query Def[/blue] in VBE help!

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Hey Ace,

Can you give me a hint on where abouts to look? All I can find is playing with queries thru the access interface. I want to create queries from VBA alone...

Something along these lines.
Code:
Set QueryDef = Object.CreateQueryDef("qryName", "sqltext")

I don't really know what to search for so no wonder I'm coming back with squat!

Thanks for the reply.
 
Got it! There may and will probably be easier ways of doing it but this is how I did it.

Code:
Dim dbsNorthwind As Database
Dim qdfNew As QueryDef
Set dbsNorthwind = CurrentDb()
With dbsNorthwind
   ' Create permanent QueryDef.
   Set qdfNew = .CreateQueryDef("qryName", "SELECT lkupAVCConnection.ID FROM lkupAVCConnection;")
   ' Open Recordset and print report.
   GetrstTemp qdfNew
   .Close
End With

And then the getrstTemp function is from Microsoft I think…

Code:
Function GetrstTemp(qdfTemp As QueryDef)
    Dim rsttemp As Recordset
    With qdfTemp
        Debug.Print .Name
        Debug.Print " " & .sql
        Set rsttemp = .OpenRecordset(dbOpenDynaset)
        Do While Not rsttemp.EOF
            Debug.Print rsttemp(0) ', rsttemp(1), rsttemp(2), rsttemp(3), rsttemp(4)
            rsttemp.MoveNext
        Loop
        With rsttemp
             .MoveLast
            Debug.Print "number of records = " & .RecordCount
            .Close
        End With
    End With
 End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top