
Hi all,
I have a unbound form with an unbound listbox on it that is populated from a union qry.
Below is the folling code i am trying to use to get it to search if I enter something in the above txt boxes.
It requeries when i enter like R in the firstname but I still get all records showing?
Its just sample data and i just cannot for the life of me figure out why this isnt populating the list box with the search
Criteria? Also on after update on all the filter txt boxes i have =FilterPerson() so this should trigger the filter click
This should work. I have it in one of my old db's and it works just fine.... Not sure what I am doing wrong.
Please do assist....Thanks
Dave
Code:
Option Compare Database
Option Explicit
Private Sub cmdReset_Click()
'Purpose: Clear all the search boxes in the Form , and show all records again.
Dim ctl As Control
'Clear all the controls in the Form section.
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acOptionGroup
ctl = Null
Case acCheckBox
ctl.Value = False
End Select
Next
'Remove the form's filter.
Me.FilterOn = False
FilterPerson
End Sub
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Dim s As String
'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Private Sub Form_Load()
'not bound -- no data is loaded
cmdReset_Click
End Sub
Private Function FilterPerson()
'this is the main procedure for filtering
Dim strWhere As String
Dim s As String
s = "SELECT PersonID, FirstName, LastName, Association, IsActive From qryPersonSearch;"
If Not IsNull(Me.FilterFirstName) Then
strWhere = strWhere & "([FirstName] LIKE ""*" & Me.FilterFirstName & "*"") And "
End If
If Not IsNull(Me.FilterLastName) Then
strWhere = strWhere & "([LastName] LIKE ""*" & Me.FilterLastName & "*"") And "
End If
If Not IsNull(Me.FilterAssociation) Then
strWhere = strWhere & "([Association] LIKE ""*" & Me.FilterAssociation & "*"") And "
End If
Debug.Print s
Me.LstPersonSearch.RowSource = s
Me.LstPersonSearch.Requery
End Function
I have tried this code also but no difference in results?
Code:
Private Function FilterPerson()
'this is the main procedure for filtering
Dim s As String, mWhere As String
s = "SELECT PersonID, FirstName, LastName, Association, IsActive From qryPersonSearch;"
If Not IsNull(Me.FilterFirstName) Then
mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "FirstName LIKE '*" & Me.FilterFirstName & "*'"
End If
If Not IsNull(Me.FilterLastName) Then
mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "LastName LIKE '*" & Me.FilterLastName & "*'"
End If
If Not IsNull(Me.FilterAssociation) Then
mWhere = (mWhere & IIf(Len(mWhere) > 0, " AND ", "")) & "Association LIKE '*" & Me.FilterAssociation & "*'"
End If
Debug.Print s
Me.LstPersonSearch.RowSource = s
Me.LstPersonSearch.Requery
End Function
...........................................................
Private Sub Form_Load()
'not bound -- no data is loaded
cmdReset_Click
End Sub
............................................................
Private Sub cmdFilter_Click()
'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
'Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
.......................................................................
Private Sub cmdReset_Click()
'Purpose: Clear all the search boxes in the Form , and show all records again.
Dim ctl As Control
'Clear all the controls in the Form section.
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acOptionGroup
ctl = Null
Case acCheckBox
ctl.Value = False
End Select
Next
'Me.grpCompanyFilter = 27
'Remove the form's filter.
Me.FilterOn = False
FilterPerson
End Sub