如何检查现有的 parent?
How to check for existing parent?
总的来说,我一直在寻找如何正确检查 object 是否已分配。
在这种特殊情况下,它是 parent 属性.
我的表格有时有 parent,并且在调整大小时的行为略有不同。
但是这段代码
If (Not Parent Is Nothing) Then
MsgBox "Is Nothing says Parent is Assigned"
Else
MsgBox "Is Nothing says Parent is not Assigned"
End If
If (IsObject(Parent)) Then
MsgBox "IsObject says Parent is Assigned"
Else
MsgBox "IsObject says Parent is not Assigned"
End If
如果有 parent,两者都可以正常工作;如果没有,则两者都会出错。
Run-time error '2452' The expression you entered has an invalid
reference to the Parent property.
是不是唯一的希望是捕获错误并在那里处理?
你可以检查一下,如果窗体是作为主窗体打开的:
If IsFormOpen(Me.Name) Then
MsgBox Me.Name & " has no parent."
Else
MsgBox Me.Name & " has a parent."
End If
' Checks if a form by name is open in the current database.
' Returns True if it does, False if not.
'
' 2011-10-10, Cactus Data ApS, Gustav Brock
'
Public Function IsFormOpen(ByVal FormName As String) As Boolean
Dim Form As Form
For Each Form In Forms
If Form.Name = FormName Then
Exit For
End If
Next
IsFormOpen = Not Form Is Nothing
End Function
如果您知道可以将表单安装为子表单的表单的名称,可以循环遍历表单集合并测试一个是否等于“父”表单名称(请参阅@Gustav 回答)或使用 IsLoaded 方法测试特定表单是否在表单集合 (If CurrentProject.AllForms("DataBituminous").IsLoaded Then
) 中打开。用作子窗体的窗体将不在窗体集合中。
如果您不想hard-code 父表单名称,则使用错误处理代码来处理丢失的父表单。示例位于 https://www.access-programmers.co.uk/forums/threads/how-to-check-if-a-form-has-a-parent-form.157642/
Private Property Get HasParent As Boolean
On error GoTo handler
HasParent = Typename(Me.Parent.Name) = "String"
Exit Property
handler:
End Property
总的来说,我一直在寻找如何正确检查 object 是否已分配。
在这种特殊情况下,它是 parent 属性.
我的表格有时有 parent,并且在调整大小时的行为略有不同。
但是这段代码
If (Not Parent Is Nothing) Then
MsgBox "Is Nothing says Parent is Assigned"
Else
MsgBox "Is Nothing says Parent is not Assigned"
End If
If (IsObject(Parent)) Then
MsgBox "IsObject says Parent is Assigned"
Else
MsgBox "IsObject says Parent is not Assigned"
End If
如果有 parent,两者都可以正常工作;如果没有,则两者都会出错。
Run-time error '2452' The expression you entered has an invalid reference to the Parent property.
是不是唯一的希望是捕获错误并在那里处理?
你可以检查一下,如果窗体是作为主窗体打开的:
If IsFormOpen(Me.Name) Then
MsgBox Me.Name & " has no parent."
Else
MsgBox Me.Name & " has a parent."
End If
' Checks if a form by name is open in the current database.
' Returns True if it does, False if not.
'
' 2011-10-10, Cactus Data ApS, Gustav Brock
'
Public Function IsFormOpen(ByVal FormName As String) As Boolean
Dim Form As Form
For Each Form In Forms
If Form.Name = FormName Then
Exit For
End If
Next
IsFormOpen = Not Form Is Nothing
End Function
如果您知道可以将表单安装为子表单的表单的名称,可以循环遍历表单集合并测试一个是否等于“父”表单名称(请参阅@Gustav 回答)或使用 IsLoaded 方法测试特定表单是否在表单集合 (If CurrentProject.AllForms("DataBituminous").IsLoaded Then
) 中打开。用作子窗体的窗体将不在窗体集合中。
如果您不想hard-code 父表单名称,则使用错误处理代码来处理丢失的父表单。示例位于 https://www.access-programmers.co.uk/forums/threads/how-to-check-if-a-form-has-a-parent-form.157642/
Private Property Get HasParent As Boolean
On error GoTo handler
HasParent = Typename(Me.Parent.Name) = "String"
Exit Property
handler:
End Property