搜索论坛后,我没有发现任何与我相似的问题/答案。
我有一个包含四个不同搜索字段的主表单 - 姓氏,城市,电话号码和ID。我希望搜索这些字段中的任何一个(或这些字段的组合),以查找具有匹配值的子表单中的所有记录 - 无论该值在哪个字段中(即子表单中有多个地址字段,因此这个城市可能会出现在这些城市中。)
此外,如果输入姓氏和城市,我只想返回包含两个值的子表单中的记录。
提前谢谢!
答案 0 :(得分:0)
我不认为如果不使用VBA就可以做到这一点。您需要在主表单上的每个搜索字段的控件的AfterUpdate
事件中添加代码,以便随后更新子表单的过滤器。我不确定允许多么复杂的过滤器,这可能是一个问题,因为它将成为过滤器的怪物。
这是一个未经测试的示例,它使用占位符来表示子窗体中潜在数量的不同字段(如您所示,它们可能是其中任意一个的倍数),并假设您的电话号码和ID是数字,您的搜索控件已进行数据验证以确保:
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