Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

Bracketing in filter string

The following filetrs work singulary but if I combine them with an 'AND' they dont work correctly:

this works:
((([Bodystyle]>='04' AND [Bodystyle]<='05') OR ([Bodystyle]='21')) OR (isnull([Bodystyle])))
this works

([Enginecapacity]>=1397 AND [Enginecapacity] <= 1397 Or Isnull([Enginecapacity]))

Open in new window

this fails

((([Bodystyle]>='04' AND [Bodystyle]<='05') OR ([Bodystyle]='21')) OR (isnull([Bodystyle]))) and ([Enginecapacity]>=1397 AND [Enginecapacity] <= 1397 Or Isnull([Enginecapacity]))

Open in new window


Not sure why.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Spell out for us in great detail how it fails.  An error message, doesn't return records, ...

Also, make sure you have your order of precedence ( )'s correct, as it looks like your filter now has a lot of expressions to deal with.
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Peter, can you state verbally what you are trying to accomplish?  

There seem to be some flaws with your logic.

For example, what are you trying to achieve with:

([Bodystyle]>='04' AND [Bodystyle]<='05')

I believe you could accomplish all of your [BodyStyle] needs (including the IsNull() with the simple statement:

NZ([Bodystyle], '04') IN ('04', '05', '21)

Which would leave you with:
 
 ([Enginecapacity]>=1397 AND [Enginecapacity] <= 1397 Or Isnull([Enginecapacity]))

But this: [Enginecapacity]>=1397 AND [Enginecapacity] <= 1397

will only evaluate to True if [EngineCapacity] = 1397, so why not just use:

Val(NZ([EngineCapacity], 1397)) = 1397

The whole criteria would read:

NZ([Bodystyle], '04') IN ('04', '05', '21) AND val(NZ([EngineCapacity], 1397)) = 1397

You may not need the Val() function in that syntax, but if this is going to be used in a query, the NZ() function will always return a string, so you need to use the Val() function to convert it back to a numeric.
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

their is variance on the engine capacity in the string i gave a variance of zero was set.

([Bodystyle]>='04' AND [Bodystyle]<='05') i wanted just bodystyle codes 04 and 05.

I will change to use the IN style.

the code doesnt give an error it just returns records I never expected.

I will try the 'IN'

it will make the filter simpler.
-->> ([Bodystyle]>='04' AND [Bodystyle]<='05')

When you're dealing with exact matches in the same field, the correct operator is OR.

(Bodystyle can't be equal to 04  AND 05 at the same time... it is 04 OR 05)

([Bodystyle] ='04' OR [Bodystyle]='05')
this is what i was doing as a whole depending how many filters are set it returns the filter string.:

If Not IsNull(Me.CBExportBody) Then
    Counter = Counter + 1
    Select Case Counter
        Case Is = 0
        Case Is = 1
                
                Select Case Me.CBExportBody.ListIndex
                    Case 0
                    'coupe /convertible / sports
                    
                    
                    strFilter = "((([Bodystyle]>='04' AND [Bodystyle]<='05') OR ([Bodystyle]='21'))" & " OR (isnull([Bodystyle])))"
                
                    
                    
                    
                    Case 1
                    'estate/mpv
                    strFilter = "(([Bodystyle]='06' OR [Bodystyle]='96')" & " OR (isnull([Bodystyle])))"
                    Case 2
                    'hatchback
                    strFilter = "(([Bodystyle]>='13' AND [Bodystyle]<='14')" & " OR (isnull([Bodystyle])))"
                    Case 3
                    'motorcycles
                    strFilter = "([Bodystyle]='72' OR [Bodystyle]='09' OR [Bodystyle]>='15' and [Bodystyle]<='19'" & " OR (isnull([Bodystyle])))"
                   
                    
                    Case 4
                    'saloon
                    strFilter = "(([Bodystyle]>='01' AND [Bodystyle]<='03')" & " OR (isnull([Bodystyle])))"
                    Case 5
                    'others
                    strFilter = "((([Bodystyle]>='07' AND [Bodystyle]<='08') OR ([Bodystyle]>='22' AND [Bodystyle]<='71'))" & " OR (isnull([Bodystyle])))"
                    
                   
                    
                End Select
                   
        
        Case Is > 1
               
                   Select Case Me.CBExportBody.ListIndex
                    Case 0
                    'coupe /convertible / sports
                    strFilter = strFilter & " and " & "((([Bodystyle]>='04' AND [Bodystyle]<='05') OR ([Bodystyle]='21'))" & " OR (isnull([Bodystyle])))"
                    Case 1
                    'estate/mpv
                    strFilter = strFilter & " and " & "(([Bodystyle]='06' OR [Bodystyle]='96')" & " OR (isnull([Bodystyle])))"
                    Case 2
                    'hatchback
                    strFilter = strFilter & " and " & "(([Bodystyle]>='13' AND [Bodystyle]<='14')" & " OR (isnull([Bodystyle])))"
                    Case 3
                    'motorcycles
                    strFilter = strFilter & " and " & "([Bodystyle]='72' OR [Bodystyle]='09' OR [Bodystyle]>='15' and [Bodystyle]<='19'" & " OR (isnull([Bodystyle])))"
                   
                    
                    Case 4
                    'saloon
                    strFilter = strFilter & " and " & "(([Bodystyle]>='01' AND [Bodystyle]<='03')" & " OR (isnull([Bodystyle])))"
                    Case 5
                    'others
                    strFilter = strFilter & " and " & "(([Bodystyle]>='07' AND [Bodystyle]<='08') OR ([Bodystyle]>='10' AND [Bodystyle]<='12') OR([Bodystyle]>='22' AND [Bodystyle]<='71')" & " OR (isnull([Bodystyle])))"
                    
                   
                    
                End Select
              
    End Select
End If

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial