The criteria I posted is supposed to be used in the criteria
row of the subform's record source query. However, as I
tried to imply earlier, this approach is not appropriate for
anything beyond simple situations and totally out of the
question for all the options you want, so let's start over.
This may seem pretty complex to you, so be sure to feel free
to retrench your needs if this is dropping you in the deep
end ;-) You will also need to check with VBA Help for
each item that you are not familiar with. I also think we
should not get involved with the OR option until we get the
AND option working.
First, we need to create a VBA procedure that creates the
subform's Filter property. To do this, we need a way for
the function to find all the criteria text boxes. This is
most easily done by naming the text boxes in a uniform way
such as txtA1, txtA2, etc.
We then need a way for the function to determine the related
column and its data type for each unbound criteria text box.
The easiest way to convey this information from the text box
to the function is to place it in the text box's Tag
property. Lets choose a syntax like fieldname:typenumber
where fieldname is the name of the field in the table and
typenumber is the code for the data type of the field
(3 - Integer, 4 - Long, 5 - Currency, 10 - Text, 12 - Memo,
etc).
With all that taken care of, the procedure in the main
form's module would look something like this air code:
Sub ApplyFilter()
Dim strWhere As String
Dim k As Integer
Dim intPos As Integer
Dim strField As String
Dim intType As Integer
For k = 1 to N ' N is number of text boxes
With Me("txtA" & k)
If Not IsNull(.Value) Then
intPos = InStr(.Tag, ":")
strField = Left(.Tag, intPos -1)
intType = Val(Mid(.Tag, intPos + 1)
strWhere = " AND " & BuildCriteria(strField, _
intType, .Value)
End If
End With
Me.subformcontrolname.Form.Filter = Mid(strWhere, 6)
Me.subformcontrolname.Form.FilterOn = True
End Sub
Now you can add a button to the main form (I'll call it the
search button) to apply the criteria by using the line of
code:
ApplyFilter
in the button's Click event procedure