【问题标题】:VBA Access check if Parent form existsVBA访问检查父表单是否存在
【发布时间】:2015-09-07 08:46:48
【问题描述】:

在 MS Acces 中,我正在从另一个对话框表单打开一个对话框表单。

所以formA,打开formB。但是他们的用户可能会单独打开formB,我想避免在这种情况下出现错误。

我考虑过检查 formB 的现有父级。

但是当我这样做时,我仍然得到错误 2452:您输入的表达式对 Parent 属性无效。

我试过了:

If Not IsError(Me.Parent) Then
    Me.Parent.cboTraining.Requery
End If

If Not IsNull(Me.Parent) Then
    Me.Parent.cboTraining.Requery
End If

【问题讨论】:

标签: ms-access ms-access-2013


【解决方案1】:

您可以使用以下方法测试表单是否打开:

If Not CurrentProject.AllForms("someFormName").IsLoaded Then

或者,当您从 formA 打开 formB 时,您可以提供一个 openArg,这很容易测试。

【讨论】:

    【解决方案2】:

    有时我的子表单是从多个表单调用的,所以我基于捕获错误的简单想法在我的 ss_utilities 模块中添加了一个通用函数:

    Public Function hasParent(ByRef p_form As Form) As Boolean
    'credit idea from https://access-programmers.co.uk/forums/showthread.php?t=157642
    On Error GoTo hasParent_error
        hasParent = TypeName(p_form.Parent.Name) = "String"
        Exit Function
    hasParent_error:
        hasParent = False
    End Function
    

    所以原始问题的代码是:

    If ss_utilities.hasParent(Me) Then
        Me.Parent.cboTraining.Requery
    End If
    

    【讨论】:

      【解决方案3】:

      这个方法和上面类似:https://stackoverflow.com/a/53582533/4924078

      Public Function hasParent(F As Object) As Boolean
      'Inspired from: https://access-programmers.co.uk/forums/showthread.php?t=293282   @Sep 10th, 2019
         Dim bHasParent As Boolean
         On Error GoTo noParents
      
         bHasParent = Not (F.Parent Is Nothing)
         hasParent = True
         Exit Function
      
      noParents:
         hasParent = False
      End Function
      

      旨在更通用,并且可以从两个子表单/子报表中调用,如下所示:

      If hasParent(Me) Then
         msgbox "Has Parent :)"
         Me.Parent.text1 = "Yes"
      else 
         msgbox "NO PARENTS .. :("
      endif
      

      【讨论】:

        【解决方案4】:

        要查找任何独立的打开表单,请尝试以下操作:

        Sub test()
        Dim i
        For i = 0 To Forms.Count - 1
            Debug.Print Forms.Item(i).Name
            If Forms.Item(i).Name Like "formA" Then MsgBox "It's Open"
        Next i
        End Sub
        

        【讨论】:

          【解决方案5】:

          这是另一个例子。

          Const conObjStateClosed = 0
          Const conDesignView = 0
          
          If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
              If Forms(strFormName).CurrentView <> conDesignView Then
                  MC_FormIsLoaded = True
              End If
          End Ifere
          

          【讨论】:

            【解决方案6】:

            这是我的解决方案。

            Public Function CheckParentFormExists(P_Form As Form) As Boolean
            On Error GoTo Err_Hendler
                Dim P_ParentForm As String
                P_ParentForm = P_Form.Parent.Name
                CheckParentFormExists = True
                
            Exit_1:
                Exit Function
            Err_Hendler:
                If Err.Number = 2452 Then
                    CheckParentFormExists = False
                    GoTo Exit_1
                Else
                    Debug.Print Err.Number & Err.Description
                End If
            End Function
            

            调用示例:

            if CheckParentFormExists(me) then
                ....
            Else
            
            End if
            

            【讨论】:

              猜你喜欢
              • 2017-11-10
              • 2011-02-28
              • 1970-01-01
              • 1970-01-01
              • 2015-08-02
              • 1970-01-01
              • 1970-01-01
              • 2013-04-27
              • 1970-01-01
              相关资源
              最近更新 更多