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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL criteria doesn't like my "Like" condition

Status
Not open for further replies.

Zygor

Technical User
Apr 18, 2001
271
US
I'm trying to get the following code to run. If the cbo box has an entry, filter it on that. Otherwise use no filter. But the no filter (nothing selected in the cbo box) isn't working. Anyone see what I'm doing wrong?

Set rstSurg = db.OpenRecordset("SELECT tblMyTable.*, tblMyTable.Dept
FROM tblMyTable
WHERE (((tblMyTable.Dept)=IIf([forms]![frmMyForm]![cboDept]<>'',[forms]![frmMyForm]![cboDept],(tblMyTable.Dept) Like '*')));")


 
If [forms]![frmMyForm]![cboDept] is = '' then the WHERE clause resolves to
Code:
WHERE tblMyTable.Dept=tblMyTable.Dept Like '*'
and that's prety much guaranteed to never happen because the LIKE clause returns a Boolean and your field is (presumably) a text string.

Maybe you need
Code:
WHERE tblMyTable.Dept LIKE IIf([forms]![frmMyForm]![cboDept]<>'',
                               [forms]![frmMyForm]![cboDept],'*')
 
One way:
Code:
Set rstSurg = db.OpenRecordset("SELECT * FROM tblMyTable WHERE Dept Like '" & Nz(Forms!frmMyForm!cboDept, "*") & "'")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top