简体   繁体   English

MS Access筛选器子表单(DataSheet)与另一个子表单(列表)

[英]MS Access Filter Subform (DataSheet) with another Subform (List)

I have a Project to be done in MS Access 2016, and stubled across an Issue, that should be easy to resolve, however, I have no clue on how to do it. 我有一个要在MS Access 2016中完成的项目,并且在一个Issue上打了个勾号,应该很容易解决,但是,我不知道该怎么做。

The Database I am developing is based on a huge, unfiltered datasheet exported by another database. 我正在开发的数据库基于另一个数据库导出的,未经过滤的庞大数据表。 I have a main form headview in which I placed two subforms listview an detailview . 我有一个主窗体headview ,其中放置了两个子窗体listviewdetailview The listview is sorted by a combobox. listview按组合框排序。

Now to what "should" happen: If you click on an entry of said listview , the detailview shows additional information of the clicked entry. 现在,“应该”发生了什么:如果单击所述listview的条目,则detailview显示单击的条目的其他信息。

Both subforms are based on the same datasheet. 这两个子窗体都基于同一数据表。 So I went ahead and tried to match them via primary key entries. 因此,我继续尝试通过主键条目匹配它们。 However, that didnt work. 但是,那没有用。 The detailview subform is still empty. detailview子表单仍然为空。 I also tried to write a vba macro for the listview with listview.click() that didnt work either. 我还尝试使用listview.click()listview编写一个vba宏,但该宏也不起作用。

Is there a way to connect those two subforms within a main form? 有没有办法在主表单中连接这两个子表单? If so, how do I do that? 如果是这样,我该怎么做?

I am greatfull for any response, 我很高兴收到任何回应,

Have a nice day -Ninsa 祝你有美好的一天-Ninsa

You should handle filtering of the detailview on the Listview_Current event. 您应该在Listview_Current事件上处理detailview的过滤。 That event fires as soon as Listview changes records. Listview更改记录后立即触发该事件。

You can either set up an event handler for the Listview_Current event on the listview's form module, or use WithEvents in the parent form to listen to that specific event. 您可以在列表视图的表单模块上为Listview_Current事件设置事件处理程序,也可以在父表单中使用WithEvents侦听该特定事件。

If you choose the latter, note that it's required that Listview has a form module, else the events won't fire. 如果选择后者,请注意Listview必须具有表单模块,否则事件将不会触发。

Given that your ID field of the datasource is ID and the detail subform control is named DetailSubformControl , this example works. 假设您的数据源的ID字段为ID ,并且详细信息子窗体控件名为DetailSubformControl ,则此示例有效。

Place this code to the Form_Current event of the listview subform (which is fired on every record you move to): 将此代码放置到listview子窗体的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

Ok, finally I got the reason for error 2455, it's a timing problem. 好的,最后我得到了错误2455的原因,这是一个计时问题。

When the procedure Form_Current of the listview form is called the first time, the detail subform is not yet bound to the detail subform control, which causes the error. 第一次调用listview表单的Form_Current过程时,detail子表单尚未绑定到detail子表单控件,这会导致错误。

Possible solutions 可能的解决方案

Option1: Ignore error 2455 选项1:忽略错误2455

Either add On Error Resume Next in the top of the Form_Current procedure or rewrite it to handle that specific error 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

Option2: Control the source objects of the subform controls 选项2:控制子窗体控件的源对象

Clear the Source Object property of the subform controls in the head form and set them explicit when loading the head form. 清除标题表单中子表单控件的“ Source Object属性,并在加载标题表单时将其显式设置。

That prevents the unlucky timing at all. 这样可以完全避免不幸的时机。

So in the head forms load event procedure add this: 因此,在头部表单加载事件过程中添加以下内容:

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

Option3: Use Link Master/Child Fields 选项3:使用链接主/子字段

You could use the properties Link Master Fields and Link Child Fields of the detail subform control. 您可以使用detail子窗体控件的属性Link Master FieldsLink Child Fields

Therefor you have to create a textbox control named ID on the head form and for cosmetic aspects hide it by setting its property Visible to False . 因此,您必须在头窗体上创建一个名为ID的文本框控件,并且为了美观,可以通过将其属性Visible设置为False来隐藏它。

This new control will be bound to the detail subform control: Set the property Link Master Fields and Link Child Fields of the detail subform control on the head form both to ID . 此新控件将绑定到detail子窗体控件:将头部窗体上的detail子窗体控件的属性Link Master FieldsLink Child Fields都设置为ID

The Form_Current procedure of the listview form then only contains this: 然后,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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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