PDA

Click to See Complete Forum and Search --> : create querry at run-time


ttingen
Aug 4th, 2000, 12:37 PM
If you want to create a QueryDef object at runtime here is an example from DAO help:


Sub CreateQueryDefX()

Dim dbsNorthwind As Database
Dim qdfTemp As QueryDef
Dim qdfNew As QueryDef

Set dbsNorthwind = OpenDatabase("Northwind.mdb")

With dbsNorthwind
' Create temporary QueryDef.
Set qdfTemp = .CreateQueryDef("", _
"SELECT * FROM Employees")
' Open Recordset and print report.
GetrstTemp qdfTemp
' Create permanent QueryDef.
Set qdfNew = .CreateQueryDef("NewQueryDef", _
"SELECT * FROM Categories")

' Open Recordset and print report.
GetrstTemp qdfNew
' Delete new QueryDef because this is a demonstration.
.QueryDefs.Delete qdfNew.Name
.Close
End With

End Sub

Function GetrstTemp(qdfTemp As QueryDef)

Dim rstTemp As Recordset

With qdfTemp
Debug.Print .Name
Debug.Print " " & .SQL
' Open Recordset from QueryDef.
Set rstTemp = .OpenRecordset(dbOpenSnapshot)

With rstTemp
' Populate Recordset and print number of records.

.MoveLast
Debug.Print " Number of records = " & _
.RecordCount
Debug.Print
.Close
End With

End With

End Function

JHausmann
Aug 4th, 2000, 12:46 PM
Sounded to me like he wanted to have one (actually 4) queries that serviced any table he creates. Like:



Function BuildSQL(tablename As String) as string

dim sSQL as string

sSQL= "select * from " & trim(tablename) & " where condition"



End Function

Sparks
Aug 4th, 2000, 07:52 PM
Exactlly right, JHausmann.
I could read the data back out, do the calc, and save the results to their own fields, although the operations are being done on 300+ point waveforms. I won't have a chance to try again till monday. Going back to the querry, I was able to sorta create querries using the db.CreateQueryDef method but they all end up in their own table. Is there a way to group them?

JHausmann
Aug 4th, 2000, 08:09 PM
Not sure on what you want to group. if the structure of your results will always be the same (ie; data returned for any group of queries is of compatible type), you could union them into a temp table