以下代码是一个类模块,并附带一个用法示例。
类模块名称= classFormOption Compare Database
Option Explicit
'21/1/2004 Added Private Set & Public Get code for frmTo.
'21/9/2004 Removed ResumeTo functionality. _
Now handled by the OnTimer() subroutine in the calling form _
checking for (Visible) which indicates the called form is finished.
'24/2/2005 Added function Uninitialised to show if instance of this object _
has yet been initialised with the callers info. _
It also checks this before it tries to open a new form.
'13/3/2007 Added optional parameter varOpenArgs to be passed 'as is' into _
the new form.
Private Const conUnInitMsg As String = _
"Object uninitialised - unable to show form."
Private frmParent As Form
Private WithEvents frmCalled As Form
Public Property Set frmFrom(frmValue As Form)
Set frmParent = frmValue
End Property
Private Property Get frmFrom() As Form
Set frmFrom = frmParent
End Property
Private Property Set frmTo(frmValue As Form)
Set frmCalled = frmValue
End Property
Public Property Get frmTo() As Form
Set frmTo = frmCalled
End Property
'Uninitialised returns True if frmFrom not yet initialised.
Public Function Uninitialised() As Boolean
Uninitialised = (frmFrom Is Nothing)
End Function
'ShowForm opens form strTo and hides the calling form. Returns True on success.
Public Function ShowForm(strTo As String, _
Optional varOpenArgs As Variant) As Boolean
ShowForm = True
'Don't even try if caller hasn't initialised Form object yet
If Uninitialised() Then
ShowForm = False
Call MsgBox(conUnInitMsg, , "classForm.ShowForm")
Exit Function
End If
Call DoCmd.Restore
'Handle error on OpenForm() only.
On Error GoTo ErrorSF
Call DoCmd.OpenForm(FormName:=strTo, OpenArgs:=varOpenArgs)
On Error GoTo 0
Set frmTo = Forms(strTo)
frmFrom.Visible = False
Exit Function
ErrorSF:
ShowForm = False
Call MsgBox("Error found in [" & frmFrom.Name & _
".ShowForm] calling [" & strTo & "]." & VbCrLf & _
"Error#=" & Err.Number & " - " & Err.Description & ".")
End Function
'************************* Contained Object Method(s) *************************
'For these subroutines to be activated the contained object must have the
''On Close' property set to a valid subroutine within the contained object.
Private Sub frmCalled_Close()
frmFrom.Visible = True
Call DoCmd.Restore
Set frmTo = Nothing
End Sub
'***************************************************************************
此代码是示例菜单形式。
它被称为另一种形式(A,B和C),并由另一个菜单(D&E)调用,因此包含了使它起作用所需的所有代码。
在我的数据库中,这是一个始终将报告最大化且始终将表单还原的标准,因此她可以执行代码,但是可以在不损失主要功能的情况下将其剥离。
- 要使用classForm类,必须对变量进行Dim引用。 由于仅在内部引用,因此只需声明为“私有”即可。
- 将该变量用于愤怒之前,需要使用其调用形式[frmFrom]进行设置。
- 调用新表格。 这将导致当前表单被隐藏,直到关闭所调用的表单为止。
- 我通常会在每个窗体上都有一个CommandButton退出。
- 如果使用了右上角的x(或其他方法之一),则实际的整理工作是(并且应该总是)在Form_Close事件过程中完成。 完全空的过程只是在编译时就删除了,因此至少必须在此处包含注释行。 如果该过程存在于被调用表单中,则该类只能获取(并继续)已关闭被调用表单的事实。
Option Compare Database
Option Explicit
Private clsTo As New classForm '<-- A
Private Sub Form_Open(Cancel As Integer)
Call DoCmd.Restore
Set clsTo.frmFrom = Me '<-- B
End Sub
Private Sub cmdGroupCust_Click()
Call clsTo.ShowForm(strTo:="frmGroupCust") '<-- C
End Sub
Private Sub cmdExit_Click() '<-- D
Call DoCmd.Close(ObjectType:=acForm, ObjectName:=Me.Name)
End Sub
Private Sub Form_Close() '<-- E
'Method must exist in order for container to handle event.
End Sub
From: https://bytes.com/topic/access/insights/615607-cascading-forms