I have tried to adapt some code I found on the microsoft site for dynamic query.
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 [LECName]= '" + Me![LECName] + "'"
where = where & " AND [Sector] = '" + Me![Sector] + "'"
where = where & " AND [Turnover] = '" + Me![Turnover] + "'"
where = where & " AND [Employees] = '" + Me![Employees] + "'"
Set QD = db.CreateQueryDef("Dynamic_Query", "select * from CompanyContacts " & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery "Dynamic_Query"
End Sub
what happens is that instead of taking the input from the drop down [LECName], [Sector] etc, nit pops up a message box asking for input.
I am very new to VBA and not sure what is going wrong, any help appreciated.
TIA
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 [LECName]= '" + Me![LECName] + "'"
where = where & " AND [Sector] = '" + Me![Sector] + "'"
where = where & " AND [Turnover] = '" + Me![Turnover] + "'"
where = where & " AND [Employees] = '" + Me![Employees] + "'"
Set QD = db.CreateQueryDef("Dynamic_Query", "select * from CompanyContacts " & (" where " + Mid(where, 6) & ";")
DoCmd.OpenQuery "Dynamic_Query"
End Sub
what happens is that instead of taking the input from the drop down [LECName], [Sector] etc, nit pops up a message box asking for input.
I am very new to VBA and not sure what is going wrong, any help appreciated.
TIA