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

问题描述 投票:0回答:2

我有子表单,有时用父表单打开,有时用父表单和祖父表单打开。

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

ms-access access-vba ms-access-2007
2个回答
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

0
投票

如果这是你唯一的两个场景,你可以做两件事。检查Mainform是否打开或检查ParentSubform1属性。

© www.soinside.com 2019 - 2024. All rights reserved.