Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic query - code acting like a parameter query

Status
Not open for further replies.

scotent

Technical User
Mar 6, 2002
16
GB
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
 
DoCmd.OpenQuery "Dynamic_Query"
the above line seems to be the problem, i think. this is the line highlighted when the debugger runs.

Any thoughts, am starting to lose the will to live.
 
Eureka! Seek and ye shall find. Nothing wrong with the code at all....

was operator confusion, I havd put in mismatching feild names for comparison in the table. Doh!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top