MS Access 过滤器子表单(数据表)与另一个子表单(列表)

MS Access Filter Subform (DataSheet) with another Subform (List)

我有一个项目要在 MS Access 2016 中完成,遇到了一个问题,应该很容易解决,但是,我不知道如何解决。

我正在开发的数据库基于另一个数据库导出的巨大的、未经过滤的数据表。我有一个主窗体 headview,其中放置了两个子窗体 listviewdetailviewlistview按组合框排序。

现在 "should" 发生了什么:如果您单击所述 listview 的条目,detailview 会显示所单击条目的附加信息。

两个子表单都基于相同的数据表。所以我继续尝试通过主键条目来匹配它们。但是,那没有用。 detailview 子表单仍然是空的。我还尝试为 listviewlistview.click() 编写一个 vba 宏,但也没有用。

有没有办法在一个主窗体中连接这两个子窗体?如果是这样,我该怎么做?

我很高兴收到任何回复,

祝你有愉快的一天 -宁萨

您应该在 Listview_Current 事件中处理 detailview 的过滤。一旦 Listview 更改记录,该事件就会触发。

您可以在列表视图的表单模块上为 Listview_Current 事件设置事件处理程序,或者在父表单中使用 WithEvents 来侦听该特定事件。

如果选择后者,注意Listview必须有表单模块,否则事件不会触发。

鉴于您的数据源 ID 字段是 ID 并且详细信息子表单控件被命名为 DetailSubformControl,此示例有效。

将此代码放入列表视图子表单的 Form_Current 事件(在您移动到的每条记录上触发):

Private Sub Form_Current()
    ' Set a reference to the detail subform control
    With Me.Parent.DetailSubformControl
        ' Set the filter of its contained form to the current ID of the listview.
        ' The "'" are only necessary if it is a text and not a numeric field.
        .Form.Filter = "[ID] = '" & Me.ID.Value & "'"
        .Form.FilterOn = True
    End With
End Sub

好的,终于找到错误2455的原因了,是时序问题

第一次调用listview窗体的过程Form_Current时,detail子窗体还没有绑定到detail子窗体控件,所以报错

可能的解决方案

选项 1:忽略错误 2455

Form_Current 过程的顶部添加 On Error Resume Next 或重写它以处理特定错误 2455:

Private Sub Form_Current()
    On Error GoTo Catch

    With Me.Parent.DetailSubformControl.Form
        .Filter = "[ID] = '" & Me.ID.Value & "'"
        .FilterOn = True
    End With

Finally:
    Exit Sub

Catch:
    If Err.Number = 2455 Then Resume Finally
    MsgBox Err.Number & "(" & Err.Description & ") occured.", vbExclamation, "Attention"
    Resume Finally
End Sub

选项2:控制子窗体控件的源对象

清除头窗体中子窗体控件的Source Object 属性,并在加载头窗体时显式设置。

这完全避免了不幸的时机。

所以在头部表单加载事件过程中添加:

Private Sub Form_Load()
    Me.DetailSubformControl.SourceObject = "Table1Detail"
    Me.DatasheetSubformControl.SourceObject = "Table1Datasheet"
End Sub

选项 3:使用 Link Master/Child 字段

您可以使用详细子表单控件的属性 Link Master FieldsLink Child Fields

因此,您必须在头部表单上创建一个名为 ID 的文本框控件,并且为了美观,通过将其 属性 Visible 设置为 False 来隐藏它。

此新控件将绑定到详细信息子窗体控件: 将head窗体的detail子窗体控件的属性Link Master FieldsLink Child Fields都设置为ID.

listview表单的Form_Current过程只包含这个:

Private Sub Form_Current()
    ' Set the value of the hidden control 'ID' on the head form,
    ' which is bound to the detail subform control, to the selected ID.
    Me.Parent.ID.Value = Me.ID.Value
End Sub