By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,705 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,705 IT Pros & Developers. It's quick & easy.

using applyfilter command to filter 2 criteria in tha same field

P: 25
hi,

i have a form name stock
i need to filter the data with this criteria :
1. status like : "yes"
2. class like : "all"
3. class like : "M2D"

i used this command but not work
Expand|Select|Wrap|Line Numbers
  1.     kr = Me.cmbstatus.Value
  2.     class1 = "all"
  3.     class2 = "M2D"
  4.  
  5.     DoCmd.ApplyFilter , "status like '" & kr & "' and class like '" & class1  & "'and class like '" & class2 & "'"
  6.  
  7.    Me.Refresh
it not work.
how do i have to filter it.

Thank you.
Feb 13 '09 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 2.5K+
P: 2,545
Hi. Assuming that in all cases Status has to be 'Yes', you need to OR the Class criteria, not AND them. However, you don't seem to be clear about what you are comparing, as you are using Like without using any wildcard characters for matching what goes before and after the string.

There is no need to use Like if you are after an exact match; the = operator is the right one for this. If you need partial matching you will need to use wildcard characters before and after the pattern, as shown below.

I have also used parenthesis for the OR part to ensure that the meaning of the statement and its execution sequence is clear. The AND is being done on the result of the OR, and the parenthesis just ensure that there is no doubt of the order in which they are interpreted.

Equals operator version
Expand|Select|Wrap|Line Numbers
  1. DoCmd.ApplyFilter , "status = '" & kr & "' AND (class = '" & class1  & "' OR class = '" & class2 & "')"
Partial match version
Expand|Select|Wrap|Line Numbers
  1. DoCmd.ApplyFilter , "status Like '*" & kr & "*' AND (class Like '*" & class1  & "*' OR class Like '*" & class2 & "*')"
-Stewart
Feb 13 '09 #2

Expert 100+
P: 634
Hi

As you have not used any wild card characters I assume you shouldn't be using Like in the filter, ie. the strings 'yes', 'all' and 'MDS' are the complete field entries.

If so, then I suggerst something like this:-

DoCmd.ApplyFilter , "status = '" & kr & "' AND (class = '" & class1 & "' OR class = '" & class2 & "')"

Note the bracket and to OR

HTH


MTB
Feb 13 '09 #3

NeoPa
Expert Mod 15k+
P: 31,411
Another way of checking multiple values against a single field is to use the In() function.
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.ApplyFilter(, "[status]='" & _
  2.                          Me.cmbstatus & _
  3.                          "' AND [class] In('all','M2D')")
  4. Me.Refresh
Feb 13 '09 #4

P: 25
well thank you guys,,,
it work...

thanks for your help.
Feb 16 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.