如何返回表单的最高父级的名称

时间:2018-04-11 19:15:35

标签: ms-access access-vba ms-access-2007

我有子表单,有时它以父表单开头,有时用父表单和祖父表格打开。

如何找到子表单当前最高父级的名称?

2 个答案:

答案 0 :(得分:1)

我建议使用事件而不是依赖于一串对象层次结构。因此subform决定需要关闭它并引发CloseRequested事件。然后,无论打开哪种形式subform都可以采取行动。这个行动可能是试图关闭自己(如果它成功那么好,它是父母)或传递它。

下面的示例不会使用事件,但会在子窗体上单击按钮时关闭父窗体。

'command button on your subform
Private Sub Command0_Click()
    Dim frm As Form
    Set frm = FindHighestAncestor(Me)
    DoCmd.Close acForm, frm.Name
End Sub

Public Function FindHighestAncestor(frm As Form)
    If IsHighestLevelForm(frm) Then
        Set FindHighestAncestor = frm
    Else
        If TypeOf frm.Parent Is Form Then
            Set FindHighestAncestor = FindHighestAncestor(frm.Parent)
        Else
            Set FindHighestAncestor = frm
        End If
    End If

End Function

Public Function IsHighestLevelForm(frm As Form) As Boolean
    Dim f As Form
    For Each f In Application.Forms
        If f.Name = frm.Name Then
            IsHighestLevelForm = True
            Exit Function
        End If
    Next
    IsHighestLevelForm = False
End Function

答案 1 :(得分:0)

如果这是你唯一的两个场景,你可以做两件事。检查Mainform是否已打开,或查看Parent的{​​{1}}属性。