在我看来,您可以使用带有以下记录源的连续表格...
SELECT Objects.ObjectName, ObjectOwners.OwnerName
FROM (ObjectOwnership INNER JOIN ObjectOwners ON ObjectOwnership.OwnerID = ObjectOwners.OwnerID) INNER JOIN Objects ON Objects.ObjectID = ObjectOwnership.ObjectID
UNION ALL
SELECT Objects.ObjectName, NULL AS OwnerName
FROM ObjectOwnership RIGHT JOIN Objects ON Objects.ObjectID = ObjectOwnership.ObjectID
WHERE ObjectOwnership.ObjectID IS NULL
ORDER BY 1, 2;
...然后在表单标题中放置几个文本框和一个“过滤器”命令按钮,以更新表单.Filter
属性以根据需要过滤记录,如下所示:
Private Sub cmdFilter_Click()
Dim sFilter As String
If IsNull(Me.txtObjectFilter.Value) And IsNull(Me.txtOwnerFilter.Value) Then
Me.FilterOn = False
Else
If IsNull(Me.txtObjectFilter.Value) Then
sFilter = ""
Else
sFilter = "ObjectName LIKE ""*" & Me.txtObjectFilter.Value & "*"""
End If
If Not IsNull(Me.txtOwnerFilter.Value) Then
If Len(sFilter) > 0 Then
sFilter = sFilter & " AND "
End If
sFilter = sFilter & "Ownername LIKE ""*" & Me.txtOwnerFilter.Value & "*"""
End If
Me.Filter = sFilter
Me.FilterOn = True
End If
End Sub
当表单打开时,它会显示所有记录...
...然后我们可以按对象过滤...
...或由所有者...
...或两者兼而有之,就此而言。
编辑
子表单的类似技术是将上面的查询另存为[ObjectOwner_base_query]
并将其指定为Record Source
子表单的,然后稍微更改 VBA 代码,以
Private Sub cmdFilter_Click()
Dim sFilter As String, sSQL As String
sFilter = ""
If Not IsNull(Me.txtObjectFilter.Value) Then
sFilter = "ObjectName LIKE ""*" & Me.txtObjectFilter.Value & "*"""
End If
If Not IsNull(Me.txtOwnerFilter.Value) Then
If Len(sFilter) > 0 Then
sFilter = sFilter & " AND "
End If
sFilter = sFilter & "Ownername LIKE ""*" & Me.txtOwnerFilter.Value & "*"""
End If
sSQL = "SELECT * FROM ObjectOwner_base_query"
If Len(sFilter) > 0 Then
sSQL = sSQL & " WHERE " & sFilter
End If
Me.RecordSource = sSQL
End Sub
(似乎修改.Filter
子窗体的属性会导致 Access 行为异常;更新.RecordSource
属性似乎会产生更好的结果。)