Place some unbound controls at the top of the form (Form Header section?)
where the user can enter the filtering values. Suggested:
- a combo containing a Value List such as 1;"Is";2;"Is Not";
- a text box where the user enters the value to match;
- a command button for Apply Filter;
- a command button for Remove Filter.
In the click event procedure of cmdApplyFilter, build the filter string.
This example shows how to apply it to a Text field.
For a Number field, remove the line:
strDelim = """"
For a Date/Time field, use:
strDelim = "#"
-----------------------code starts-------------------
Dim strField As String 'Name of field to filter on.
Dim strDelim As String 'Delimiter for type of field.
Dim strFilter As String
strField = "[Surname]"
strDelim = """"
If IsNull(Me.cboOp) Or IsNull(Me.txtFind) Then
MsgBox "Must enter both combo and text box values."
Else
If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
'Create the filter string
Select Case Me.cboOp
Case 1 'Is
strFilter = strField & " = " & strDelim & Me.txtFind & strDelim
Case 2 'Is Not
strFilter = strField & " <> " & strDelim & Me.txtFind & strDelim
Case Else
Debug.Print "Combo value not handled."
End Select
'Apply the filter
If Len(strFilter) > 0 Then
Me.Filter = strFilter
Me.FilterOn = True
End If
End If
-----------------------code ends-------------------
The code is written so it is very easy to extend. For example you could
provide an option in the combo for "Begins with", and in that case:
strFilter = strField & " Like " & strDelim & Me.txtFind & "*" & strDelim
Or an option for "Blank", and handle the case as:
strFilter = strField & " Is Null"
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
debraj007 said:
I have a main form based on a table. I would like to give the users the
option to show only records with a certain field entry, records without
this
certain entry, and all records by choosing the appropriate button. Any
ideas?