Hi, I'm trying to filter a subform based on 4 comboboxes in the main form, which are:
City
State
Zip Code
Area
Basically, i want to search mother's based on these 4 comboboxes, so that the subform would show the filtered mom.
However, the code that I've been using keeps giving me error. Can you help me take a look at these? I've working on these for like 1 week and it's getting me no where!
Here is the code that I've been using:
[/code]
Private Function MomSearch()
On Error GoTo Error_MomSearch
Dim CurrentFilter As String
Dim FilterClause As String, D As Long
'Hold whether we use AND or OR in our filter criteria
D = Me.DirectionGrp.Value
'1st Combo- UpdatedMotherCity - The Table Field 'UpdatedMotherCity' is of a text datatype
If Nz(Me.cbxMomCity.Column(0), 0) > 0 Then
If FilterClause <> "" Then FilterClause = FilterClause & IIf(D = 1, " AND ", " OR ")
FilterClause = FilterClause & "[cbxMomCity]='" & Me.cbxMomCity.Value & "'"
End If
'2nd Combo- UpdatedMomState - The Table Field 'UpdatedMotherCity' is of a text datatype
If Nz(Me.cbxMomState.Column(0), 0) > 0 Then
If FilterClause <> "" Then FilterClause = FilterClause & IIf(D = 1, " AND ", " OR ")
FilterClause = FilterClause & "[cbxMomState]='" & Me.cbxMomState.Value & "'"
End If
'3rd Combo- UpdatedMomZip - The Table Field 'UpdatedMotherCity' is of a text datatype
If Nz(Me.cbxMomZip.Column(0), 0) > 0 Then
If FilterClause <> "" Then FilterClause = FilterClause & IIf(D = 1, " AND ", " OR ")
FilterClause = FilterClause & "[cbxMomZip]='" & Me.cbxMomZip.Value & "'"
End If
'4th Combo- Area - The Table Field 'Area' is of a text datatype
If Len(Me.cbxMomArea.Value & "") > 0 Then
If FilterClause <> "" Then FilterClause = FilterClause & IIf(D = 1, " AND ", " OR ")
FilterClause = FilterClause & "[cbxMomArea]='" & Me.cbxMomArea.Value & "'"
End If
'Fill this form wide variable so that it can be used for the report
CurrentFilter = FilterClause: FilterClause = ""
'Place our created filter criteria into the filter property of subform.
Forms("MailMerge")("MailMergeQuery").Form.Filter = CurrentFilter
'Turn OFF the filter
Forms("MailMerge")("MailMergeQuery").Form.FilterOn = True
Exit_MomSearch:
Exit Function
Error_MomSearch:
MsgBox "MomSearch function Error" & vbCr & vbCr & _
Err.Number & " - " & Err.Description, vbExclamation, _
"Mothers Search Error"
Resume Exit_MomSearch
End Function
[/code]
AccessQuestion.zipI've also attached the file in this post.
Please ignore the 3 errors it gives asking for mother's name and address when you first open the file. I have to delete this information to protect the ID of the mothers.
Thank you,
Meo