简体   繁体   中英

Form Search to find all records with value in subform (regardless of field value in)

After searching the forum, I haven't found any questions/answers quite like mine.

I have a main form with four different search fields - last name, city, phone number, and ID. I want to have a search of any of these fields (or combination of these fields) to find all records in the subform with a matching value - regardless of which field that value is in (ie there are multiple address fields in the subform, so the city could appear in any of these).

Additionally, if a Last Name AND City are entered I only want to return records in the subform that include both values.

Thank you in advance!

I don't think this can be done without employing VBA. You would need to add code to the AfterUpdate event for each search field's control on the main form that would subsequently update the subform's filter. I'm not sure how complex filters are allowed to be, though, which could be a problem because it's going to be a monster of a filter.

Here's an untested example that uses placeholders for the potential number of different fields in the subform (as you indicated there could be multiples of any of them) and assuming your phone number and ID are numbers and that your search controls have data validation in place to ensure that:

Private Sub last_name_AfterUpdate()
    Call FilterSubForm()
End Sub

Private Sub city_AfterUpdate()
    Call FilterSubForm()
End Sub

Private Sub phone_number_AfterUpdate()
    Call FilterSubForm()
End Sub

Private Sub ID_AfterUpdate()
    Call FilterSubForm()
End Sub

Private Sub FilterSubForm()
    Dim sLastName As String
    Dim sCity As String
    Dim sPhone As String
    Dim sID As String
    Dim sFilter As String

    sLastName = Trim(Me.[last name])
    sCity = Trim(Me.city)
    sPhone = Trim(Me.[phone number])
    sID = Trim(Me.ID)

    If sLastName != "" And sCity != "" Then     
            sFilter = "(([last_name_1] = '" & sLastName & "' " _
                            & "OR [last_name_2] = '" & sLastName & "' " _
                            & "OR [last_name_etc] = '" & sLastName & "') " _
                            & "AND " _
                            & "([city_1] = '" & sCity & "' " _
                            & "OR [city_2] = '" & sCity & "' " _
                            & "OR [city_etc] = '" & sCity & "'))"
            If sPhone != "" Then
                    sFilter = sFilter _
                                    & " AND " _
                                    & "[phone_number_1] = " & sPhone & " " _
                                    & "OR [phone_number_2] = " & sPhone & " " _
                                    & "OR [phone_number_etc] = " & sPhone
            End If
            If sID != "" Then
                    sFilter = sFilter _
                                    & IIf(sPhone != "", " OR ", " AND ") _
                                    & "[ID_1] = " & sID & " " _
                                    & "OR [ID_2] = " & sID & " " _
                                    & "OR [ID_etc] = " & sID
            End If
    Else
            If sLastName != "" Then
                    sFilter = "[last_name_1] = '" & sLastName & "' " _
                                    & "OR [last_name_2] = '" & sLastName & "' " _
                                    & "OR [last_name_etc] = '" & sLastName & "'"
            End If
            If sCity != "" Then
                    sFilter = sFilter _
                                    & IIf(sLastName != "", " OR ", "") _
                                    & "[city_1] = '" & sCity & "' " _
                                    & "OR [city_2] = '" & sCity & "' " _
                                    & "OR [city_etc] = '" & sCity & "'"
            End If
            If sPhone != "" Then
                    sFilter = sFilter _
                                    & IIf(sCity != "", " OR ", "") _
                                    & "[phone_number_1] = " & sPhone & " " _
                                    & "OR [phone_number_2] = " & sPhone & " " _
                                    & "OR [phone_number_etc] = " & sPhone                                       
            End If
            If sID != "" Then
                    sFilter = sFilter _
                                    & IIf(sPhone != "", " OR ", "") _
                                    & "[ID_1] = " & sID & " " _
                                    & "OR [ID_2] = " & sID & " " _
                                    & "OR [ID_etc] = " & sID                                        
            End If
    End If

    Me.[your_subform].Filter = sFilter
            Me.[your_subform].FilterOn = True
    Me.[your_subform].Requery
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM