【问题标题】:How to return the name of a form's highest parent如何返回表单最高父级的名称
【发布时间】:2018-09-21 19:12:22
【问题描述】:

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

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

【问题讨论】:

  • 关闭子表单是什么意思?
  • 对不起,我要么按照上面的描述打开它,那里有父母和祖父母。或者我用类似于 Subform1 作为主窗体和 Subsubform1 作为子窗体的窗体打开它,在这种情况下只有一个父级,没有祖父级。

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


【解决方案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

【讨论】:

    【解决方案2】:

    如果这是您仅有的两种情况,您可以做两件事。检查Mainform 是否打开或检查Subform1Parent 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多