搜索论坛后,我没有找到任何像我一样的问题/答案。
我有一个包含四个不同搜索字段的主表单——姓氏、城市、电话号码和 ID。我想搜索这些字段中的任何一个(或这些字段的组合)以查找子表单中具有匹配值的所有记录 - 无论该值在哪个字段中(即子表单中有多个地址字段,所以城市可能出现在其中任何一个中)。
此外,如果输入姓氏和城市,我只想返回包含这两个值的子表单中的记录。
先感谢您!
我认为不使用 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