I usually distribute my applications as MDE's but am not adverse to using query objects for processing. The following code sits in a module. In this case I would call the function ExtractClasses to create/recreate the query object each time it is required, then use docmd.openquery "qryExtract_Classes" to actually run it.
That way, I can use query objects (as Access is designed to do) but am safe in the knowledge that if anybody does butcher a query that each time they run my code I am using the query that I actually created ... because my query is recreated or created each run.
When my database closes, I also delete all query objects and table links (I link tables on opening a database as well ... just in case somebody drops a table on me).
Sub ExtractClasses()
On Error GoTo ExtractClasses_Err
Dim dbs As Database, qdf As QueryDef
Dim strSQL As String, qryTag As String
' define query object name
qryTag = "qryExtract_Classes"
' refresh query collection
Set dbs = CurrentDb
dbs.QueryDefs.Refresh
' If query exists, delete it.
For Each qdf In dbs.QueryDefs
If qdf.Name = qryTag Then
dbs.QueryDefs.Delete qdf.Name
End If
Next qdf
strSQL = "Your query code here;"
' Create new QueryDef object.
Set qdf = dbs.CreateQueryDef(qryTag, strSQL)
ExtractClasses_Bye:
' release objects
qdf.Close
dbs.Close
Set qdf = Nothing
Set dbs = Nothing
Exit Sub
ExtractClasses_Err:
MsgBox Err.Description
Resume ExtractClasses_Bye
End Sub
Alternatively, you can create a SQL string and then docmd.runsql "MyString" to run your query. I like the first option better because it looks less messy in the "processing" function - just a one line call.
Cheers
![[pipe] [pipe] [pipe]](/data/assets/smilies/pipe.gif)