I have the following code attached to a command button, it takes its input from four drop down boxes. What I would like to do is:
a) allow for a null value i.e. if nothing is selected in the dropdown
and b) have the resultant recordset appear in the main body of the form as a datasheet view.
The dropdowns and command button are positioned in the head section of the form.
Private Sub cmdRunQuery_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As Variant
Set db = CurrentDb()
'Delete existing dynamic query; trap the error if the query does not exist
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query"
On Error GoTo 0
where = Null
where = where & " AND [LECID]= '" + Me![LECName] + "'"
where = where & " AND [BusinessSector] = '" + Me![Sector] + "'"
where = where & " AND [Turnover] = '" + Me![Turnover] + "'"
where = where & " AND [NumberOfEmployees] = '" + Me![Employees] + "'"
Set QD = db.CreateQueryDef("Dynamic_Query", "select * from CompanyContacts " & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery ("Dynamic_Query"
End Sub
TIA
a) allow for a null value i.e. if nothing is selected in the dropdown
and b) have the resultant recordset appear in the main body of the form as a datasheet view.
The dropdowns and command button are positioned in the head section of the form.
Private Sub cmdRunQuery_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As Variant
Set db = CurrentDb()
'Delete existing dynamic query; trap the error if the query does not exist
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query"
On Error GoTo 0
where = Null
where = where & " AND [LECID]= '" + Me![LECName] + "'"
where = where & " AND [BusinessSector] = '" + Me![Sector] + "'"
where = where & " AND [Turnover] = '" + Me![Turnover] + "'"
where = where & " AND [NumberOfEmployees] = '" + Me![Employees] + "'"
Set QD = db.CreateQueryDef("Dynamic_Query", "select * from CompanyContacts " & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery ("Dynamic_Query"
End Sub
TIA