Here's the entire code for this button. I have about 20 unbound controls
(combo, list, and text boxes) in the header of the form for filtering
experimental data based on 3 tables: MLOBOOK (sample catalog), DATA, and
Conditions. I based this on Allen's filter form example and filtering
based
on a field in a subform. When I select only values in fProperty my form
pops
up a message that says "No Criteria". Also debug.print PropertyLen
returns -2
in the immediate window. I'm also trying to have this button add to the
filter string using "OR" if the form is already filtered rather than
starting
from scratch every time. Thanks for your help.
Marcie
Private Sub cmdFilterAnd_Click() 'Adapted from
http://allenbrowne.com/ser-62code.html
Dim segCND As Long
Dim segNum As Long
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#mm\/dd\/yyyy\#"
segNum = 0
Dim strCND As String
Dim lenCND As Long
If Me.FilterOn = True Then
strWhere = Me.Filter
strWhere = "(" & strWhere & ") OR ("
segNum = 1
'Debug.Print strWhere
End If
strCND = Me.RecordSource
lenCND = Len(strCND)
'Debug.Print lenCND
If lenCND > 654 Then
strCND = Right(strCND, (lenCND - 654))
strCND = Left(strCND, Len(strCND) - 3)
'Debug.Print strCND
strCND = "(" & strCND & ") OR ("
segCND = 1
Else
strCND = ""
segCND = 0
End If
Dim strSQL As String
If IsNull(Me.fcboCnd1) And segCND = 0 Then
' If the combo is Null, use the whole table as the
RecordSource.
Me.RecordSource = "SELECT MLOBOOK.Description,
Data.CatalogYear,
Data.CatalogPrefix, Data.CatalogID, " & _
"Data.DateTested, Data.Property, Data.TestMethod,
Data.RNumeric,
Data.RText, Data.Units, Data.Comments, " & _
"Data.TestRunBy, Data.TestAssignedTo, Data.Publication,
Data.TestAssignedDate, Data.ApprovedBy, Data.ID " & _
"FROM MLOBOOK INNER JOIN Data ON (MLOBOOK.CatalogID =
Data.CatalogID) AND (MLOBOOK.CatalogYear = Data.CatalogYear) " & _
"AND (MLOBOOK.CatalogPrefix = Data.CatalogPrefix) WHERE
(((Data.CatalogYear) Is Not Null));"
Else
If Not IsNull(Me.fcboCnd1) Then
strCND = strCND & "((Conditions.ConditionName) Like """ &
Me.fcboCnd1 & """) And "
If Not IsNull(Me.fCnd1L) Then
strCND = strCND & "((Conditions.Value) >= " & Me.fCnd1L
& ") And "
End If
If Not IsNull(Me.fCnd1H) Then
strCND = strCND & "((Conditions.Value) <= " & Me.fCnd1H
& ") And "
End If
If Not IsNull(Me.fcboCndUnits1) Then
strCND = strCND & "((Conditions.Units) Like """ &
Me.fcboCndUnits1 & """) And "
End If
End If
If Not IsNull(Me.fcboCnd2) Then
strCND = "((Conditions.ConditionName) Like """ &
Me.fcboCnd2
& """) And "
If Not IsNull(Me.fCnd2L) Then
strCND = strCND & "((Conditions.Value) >= " & Me.fCnd2L
& ") And "
End If
If Not IsNull(Me.fCnd2H) Then
strCND = strCND & "((Conditions.Value) <= " & Me.fCnd2H
& ") And "
End If
If Not IsNull(Me.fcboCndUnits2) Then
strCND = strCND & "((Conditions.Units) Like """ &
Me.fcboCndUnits2 & """) And "
End If
End If
If Not IsNull(Me.fcboCnd3) Then
strCND = "((Conditions.ConditionName) Like """ &
Me.fcboCnd3
& """) And "
If Not IsNull(Me.fCnd3L) Then
strCND = strCND & "((Conditions.Value) >= " & Me.fCnd3L
& ") And "
End If
If Not IsNull(Me.fCnd3H) Then
strCND = strCND & "((Conditions.Value) <= " & Me.fCnd3H
& ") And "
End If
If Not IsNull(Me.fcboCndUnits3) Then
strCND = strCND & "((Conditions.Units) Like """ &
Me.fcboCndUnits3 & """) And "
End If
End If
lenCND = Len(strCND) - 5
strCND = Left(strCND, lenCND)
'Debug.Print strCND
If segCND = 1 Then
strCND = strCND & ")"
End If
strSQL = "SELECT MLOBOOK.Description, Data.CatalogYear,
Data.CatalogPrefix, Data.CatalogID, " & _
"Data.DateTested, Data.Property, Data.TestMethod,
Data.RNumeric,
Data.RText, Data.Units, Data.Comments, " & _
"Data.TestRunBy, Data.TestAssignedTo, Data.Publication,
Data.TestAssignedDate, Data.ApprovedBy, Data.ID " & _
"FROM Conditions INNER JOIN ((MLOBOOK INNER JOIN Data ON
(MLOBOOK.CatalogID = Data.CatalogID) AND (MLOBOOK.CatalogYear " & _
"= Data.CatalogYear) AND (MLOBOOK.CatalogPrefix =
Data.CatalogPrefix)) INNER JOIN ConditionsDataJunction " & _
"ON Data.ID = ConditionsDataJunction.DataID) ON Conditions.ID =
ConditionsDataJunction.ConditionsID " & _
"WHERE (((Data.CatalogYear) Is Not Null) AND (" & strCND &
"));"
'Debug.Print strSQL
Me.RecordSource = strSQL
End If
Dim strPrefix As String
Dim strYear As String
Dim strID As String
If Not IsNull(Me.cboMLOL) And IsNull(Me.cboMLOH) Then
strPrefix = Me.cboMLOL
strPrefix = Left(strPrefix, 3)
strYear = Me.cboMLOL
strYear = Right(strYear, 9)
strYear = Left(strYear, 4)
strID = Me.cboMLOL
strID = Val(Right(strID, 4))
'Debug.Print strPrefix
'Debug.Print strYear
'Debug.Print strID
strWhere = strWhere & "(([CatalogPrefix] Like """ & strPrefix &
""")
and "
strWhere = strWhere & "([CatalogYear] >= " & strYear & ") and "
strWhere = strWhere & "([CatalogID] >= " & strID & ")) AND "
End If
If Not IsNull(Me.cboMLOH) And IsNull(Me.cboMLOL) Then
strPrefix = Me.cboMLOH
strPrefix = Left(strPrefix, 3)
strYear = Me.cboMLOH
strYear = Right(strYear, 9)
strYear = Left(strYear, 4)
strID = Me.cboMLOH
strID = Val(Right(strID, 4))
'Debug.Print strPrefix
'Debug.Print strYear
'Debug.Print strID
strWhere = strWhere & "(([CatalogPrefix] Like """ & strPrefix &
""")
and "
strWhere = strWhere & "([CatalogYear] <= " & strYear & ") and "
strWhere = strWhere & "([CatalogID] <= " & strID & ")) AND "
End If
If Not IsNull(Me.cboMLOH) And Not IsNull(Me.cboMLOL) Then
strPrefix = Me.cboMLOL
strPrefix = Left(strPrefix, 3)
strYear = Me.cboMLOL
strYear = Right(strYear, 9)
strYear = Left(strYear, 4)
strID = Me.cboMLOL
strID = Val(Right(strID, 4))
'Debug.Print strPrefix
'Debug.Print strYear
'Debug.Print strID
strWhere = strWhere & "(([CatalogPrefix] Like """ & strPrefix &
""")
and "
strWhere = strWhere & "([CatalogYear] >= " & strYear & ") and "
strWhere = strWhere & "([CatalogID] >= " & strID & ")) and "
strPrefix = Me.cboMLOH
strPrefix = Left(strPrefix, 3)
strYear = Me.cboMLOH
strYear = Right(strYear, 9)
strYear = Left(strYear, 4)
strID = Me.cboMLOH
strID = Val(Right(strID, 4))
'Debug.Print strPrefix
'Debug.Print strYear
'Debug.Print strID
strWhere = strWhere & "(([CatalogPrefix] Like """ & strPrefix &
""")
and "
strWhere = strWhere & "([CatalogYear] <= " & strYear & ") and "
strWhere = strWhere & "([CatalogID] <= " & strID & ")) and "
End If
'Debug.Print strWhere
'Loop through the ItemsSelected in the list box.
Dim strProperty As String, PropertyLen As Long, varItem As Variant,
strQ As String
strQ = """"
With Me.fProperty
For Each varItem In .ItemsSelected
'Build up the filter from the bound column (hidden).
strProperty = strProperty & strQ & .Column(varItem) &
strQ & ", "
Next varItem
End With
Debug.Print strProperty
PropertyLen = Len(strProperty) - 2
Debug.Print PropertyLen
If PropertyLen > 0 Then
strProperty = "([Property] IN (" & Left$(strProperty,
PropertyLen) & "))"
strWhere = strWhere & strProperty & " AND "
End If
'Remove trailing comma. Add field name, IN operator, and
brackets.
'Loop through the ItemsSelected in the list box.
Dim strMethod As String, methodlen As Long
With Me.fMethod
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strMethod = strMethod & strQ & .ItemData(varItem) & strQ &
","
End If
Next
End With
methodlen = Len(strMethod) - 1
If methodlen > 0 Then
strMethod = "[TestMethod] IN (" & Left$(strMethod, methodlen) &
")"
End If
If Len(strWhere) > 0 And methodlen > 0 Then
strWhere = strWhere & "(" & strMethod & ") AND "
ElseIf Len(strWhere) <= 0 And methodlen > 0 Then
strWhere = strMethod & " AND "
Else
strWhere = strWhere
End If
'Remove trailing comma. Add field name, IN operator, and
brackets.
'Deselect and clear filter controls.
If Not IsNull(Me.fResultL) Then
strWhere = strWhere & "([RNumeric] >= " & Me.fResultL & ") AND
"
End If
If Not IsNull(Me.fResultH) Then
strWhere = strWhere & "([RNumeric] <= " & Me.fResultH & ") AND
"
End If
'Loop through the ItemsSelected in the list box.
Dim strText As String, TextLen As Long
With Me.fResultText
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strText = strText & strQ & .ItemData(varItem) & strQ &
","
End If
Next
End With
TextLen = Len(strText) - 1
If TextLen > 0 Then
strText = "[RText] IN (" & Left$(strText, TextLen) & ")"
End If
If Len(strWhere) > 0 And TextLen > 0 Then
strWhere = strWhere & "(" & strText & ") AND "
ElseIf Len(strWhere) <= 0 And TextLen > 0 Then
strWhere = strText & " AND "
Else
strWhere = strWhere
End If
'Add quotes and astricts to make any, all, or exact phrase
strings from description.
Dim strDescription As String, strAny As String, strAll As String,
strAst
As String
strAst = "*"
strAny = strAst & strQ & " OR [Description] Like " & strQ & strAst
strAll = strAst & strQ & " AND [Description] Like " & strQ & strAst
If Not IsNull(Me.fDescription) Then
strDescription = Me.fDescription
'Debug.Print strDescription
If (Me.optDesc) = 1 Then
strDescription = Replace(strDescription, " ", strAny)
strWhere = strWhere & "([Description] Like ""*" & strDescription &
"*"") AND "
ElseIf (Me.optDesc) = 2 Then
strDescription = Replace(strDescription, " ", strAll)
strWhere = strWhere & "([Description] Like ""*" & strDescription &
"*"") AND "
ElseIf (Me.optDesc) = 3 Then
strWhere = strWhere & "([Description] Like ""*" & strDescription &
"*"") AND "
'Debug.Print strWhere
End If
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 And IsNull(Me.fcboCnd1) And IsNull(Me.fcboCnd2) And
IsNull(Me.fcboCnd3) Then
MsgBox "No Criteria", vbInformation, "Nothing to do."
ElseIf lngLen > 0 Then
Call Reset(Me)
strWhere = Left$(strWhere, lngLen)
Me.Filter = strWhere
Me.FilterOn = True
Me.FilterString.ForeColor = vbBlack
End If
On Error GoTo Err_cmdFilterAnd_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 2, , acMenuVer70
Exit_cmdFilterAnd_Click:
Exit Sub
Err_cmdFilterAnd_Click:
MsgBox Err.Description
Resume Exit_cmdFilterAnd_Click
End Sub
Douglas J. Steele said:
The code you've presented is syntactically correct (which is to be
expected
from Allen's work).
How are you using that code? How are you determining that nothing's being
returned?
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
bymarce said:
I have a filter form (based on Allen Browne's code). This portion of
the
code is supposed to build a string based on a multiselect list box but
it's
returning nothing no matter how many items are selected. Why isn't
this
working? Thanks.
Marcie
'Loop through the ItemsSelected in the list box.
Dim strProperty As String, PropertyLen As Long, varItem As
Variant,
strQ As String
strQ = """"
With Me.fProperty
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strProperty = strProperty & strQ & .Column(0,
varItem)
&
strQ & ", "
End If
Next
End With
Debug.Print strProperty
PropertyLen = Len(strProperty) - 2
Debug.Print PropertyLen
If PropertyLen > 0 Then
strProperty = "([Property] IN (" & Left$(strProperty,
PropertyLen) & "))"
strWhere = strWhere & strProperty & " AND "
End If