I have a form with several controls that I'm using to filter its records. To make it simple, let's say the controls are cboCat, chkA, chkB and cmdSearch. In reality there are several more combos and everything worked perfectly until I added the check boxes.
This is the simplified code I've got:
In the form's module:
Code:
Private Sub cmdSearch_Click()
Dim lngCatID As Long, blnA As Boolean, blnB As Boolean
If (Me!cboCat = 0) And (Me!chkA = False) And (Me!chkB = False) Then
MsgBox "Please select something to search by."
Else
lngCatID = Nz(Me!cboCat)
blnA = Me!chkA,
blnB = Me!chkB
strFilter = GetFilterString(lngCatID, blnA, blnB)
Me.Filter = strFilter
Me.FilterOn = True
End If
End Sub
In a standard module:
Code:
Public Function GetFilterString(ByVal lngCatID As Long, _
ByVal blnA As Boolean, _
ByVal blnB As Boolean) As Variant
Dim strFilter As String
MsgBox blnA & " " & blnB
If (lngCatID > 0) And (blnA = False) And (blnB = False) Then 'Case 1
strFilter = "CatID = " & lngCatID
ElseIf (lngCatID = 0) And (blnA = True) And (blnB = False) Then 'Case 2
strFilter = "A = " & blnA
ElseIf (lngCatID = 0) And (blnA = False) And (blnB = True) Then 'Case 3
strFilter = "B = " & blnB
End If
MsgBox strFilter
GetFilterString = strFilter
End Function
In all three cases, the first MsgBox in the function gives the correct values and the second MsgBox shows the correct filter string in Case 1. But in Cases 2 & 3, nothing appears in the second MsgBox and no filtering happens on the form. I've also tried the "...And blnA And Not blnB..." style with the same result. What am I doing wrong?