Correct way to express filter in VBA

mrmozambique

Registered User.
Local time
Today, 18:35
Joined
May 20, 2009
Messages
16
Hi all. I struggle with this every time I try to do it and it seems that I can never find the solution online. I have an unbound combo in the header of a continuous form with a list of districts (text). I want to filter the form based on the text field [DistrictName]. I have the following code in the AfterUpdate on the district combo. Changing the form doesn't filter at all in some, returns zero in another, or asks for the value in the third. What am I doing wrong here?


Code:
    Dim strDist As String                  'The criteria string.
    Dim strDistName
    If Not IsNull(Me.cbDistrictFilter) Then
'WHICH OF THESE OPTIONS CORRECT OR ARE ANY CORRECT?
'        strDist = "([DistrictName] = " & Me.cbDistrictFilter & ") "
'        strDist = strDist & "([DistrictName] = " & Me.cbDistrictFilter & ") "
'        strDist = strDist & "[DistrictName] = " & Me.cbDistrictFilter
'        strDist = strDist & "[DistrictName] = Me.cbDistrictFilter"
    End If
 
Since they are strings, surround them in single Quotes.. Are you setting the filter of the form?
Code:
strDist = "[DistrictName] = '" & Me.cbDistrictFilter & "'"
 
Since they are strings, surround them in single Quotes.. Are you setting the filter of the form?
Code:
strDist = "[DistrictName] = '" & Me.cbDistrictFilter & "'"

You don't know how long I've been doing this - :banghead: because of these quotes. Thanks a ton. This did the trick.
 
Most welcome.. Sometime the little things are the ones that keep us up.. :) Glad to help..

Just a small (quick) tutorial..
* Strings always needs to be enclosed within (Single) Quotes.
Code:
strDist = "[DistrictName] = [COLOR=Red][B]'[/B][/COLOR]" & Me.cbDistrictFilter & "[COLOR=Red][B]'[/B][/COLOR]"
* Numbers do not need to be wrapped..
Code:
strDist = "[DistrictName] = " & Me.cbDistrictFilter
* Dates should be enclosed within ##
Code:
strDist = "[DistrictName] = [COLOR=Red][B]#[/B][/COLOR]" & Me.cbDistrictFilter & "[COLOR=Red][B]#[/B][/COLOR]"
Hope this helps..
 

Users who are viewing this thread

Back
Top Bottom