I'm putting together an emergency contacts form for my company, and I'm having trouble with an automatic filter I'm trying to add:
I have a drop down for Department, and a text box for name. For the Department filter, I'm using an afterUpdate action, and for the Name filter, I'm using a onKeyUp action. They both work fine independently, and through a lot of trial and error, I got it to work when you type a name, then select a department, but for the life of me, I cannot get it to work when you select the department first, and then type a name. Any help would be greatly appreciated. Here's the code I'm using:
Code:
Private Sub Combo23_AfterUpdate()
On Error GoTo Proc_Error
Dim departfilter As String
Dim namefilter As String
departfilter = "[Department]=" & "Combo23"
namefilter = "[EmpName] LIKE '*" & Trim(Text27.Value) & "*'"
If (Len(Trim(departfilter)) = 0) Or (Len(Trim(namefilter)) = 0) Then
Me.ECONsf.Form.Filter = departfilter & namefilter
Else
Me.ECONsf.Form.Filter = departfilter & " And " & namefilter
End If
Me.ECONsf.Form.FilterOn = True
Proc_Exit:
Exit Sub
Proc_Error:
MsgBox "Error " & Err.Number & " in setting subform filter:" & vbCrLf & Err.Description
Resume Proc_Exit
End Sub
Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo Proc_Error
Dim departfilter As String
Dim namefilter As String
departfilter = "[Department]=" & "Combo23"
namefilter = "[EmpName] LIKE '*" & Trim(Text27.Value) & "*'"
If (Len(Trim(departfilter)) = 0) Or (Len(Trim(namefilter)) = 0) Then
Me.ECONsf.Form.Filter = departfilter & namefilter
Else
Me.ECONsf.Form.Filter = departfilter & " And " & namefilter
End If
Me.ECONsf.Form.FilterOn = True
Proc_Exit:
Exit Sub
Proc_Error:
MsgBox "Error " & Err.Number & " in setting subform filter:" & vbCrLf & Err.Description
Resume Proc_Exit
End Sub