I feed dropdown list information from a form to this code to create and execute a dynamic_query in Access MS Office 2000. All this works fine but I need additional code (wording and format) to be able to add GROUP BY and ORDER BY information (provided from the form) to the query. Can anybody supply the wording in this code set for me to be able to do this?
This is the sample code.
This is the sample code.
Code:
Private Sub SetCriteria_Click()
On Error GoTo Err_SetCriteria_Click
'This is the Search Engine routine.
Dim db As Database
Dim QD As QueryDef
Dim where As Variant
Set db = DBEngine.Workspaces(0).Databases(0)
'Delete existing dynamic query, trap error if it does not exist.
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query")
On Error GoTo 0
'Note Single quotes surrounding text fields [Territory]
'Note NO Single quotes surrounding any Numeric field [terr]
'This section sets the criteria for the dynamic_query
where = Null
where = where & (" AND [connum]")
where = where & (" AND [ACCT]= '" + Me![Account] + "'")
where = where & (" AND [CATEGORY]= '" + Me![CATEGORY] + "'")
where = where & (" AND [Expires]= '" + Me![Expire Date] + "'")
'MsgBox "Select * from contract " & (" where " + Mid(where, 6) & ";")
'Remove previous line to NOT have the SQL statement displayed
Set QD = db.CreateQueryDef("Dynamic_Query", "Select * from Depreciation " & (" where " + Mid(where, 6) & ";"))
DoCmd.ApplyFilter "Dynamic_Query"
Dim stDocName As String
stDocName = "Dynamic_Query"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_SetCriteria_Click:
Exit Sub
Err_SetCriteria_Click:
MsgBox Err.DESCRIPTION
Resume Exit_SetCriteria_Click
End Sub
Comment