总览
本文介绍如何设置和使用类模块,以及更具体地说,如何打开窗体,使其以分层菜单结构的形式出现。
窗体A打开窗体B ,而窗体B仍处于打开状态,但从视图中隐藏起来。 随着窗体B终止, 窗体A重新出现并重新确定焦点。的代码
下面的classForm包括设置类属性以及各种方法的示例。 这些方法中特别令人感兴趣的是封装的事件处理程序frmCalled_Close() ,由于第19行中使用了WithEvents关键字,因此可以触发该事件处理程序(请参见下面的代码)。 模块代码Option 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.
'31/3/2008 Added varOpenArgs as optional parameter to ShowForm. Simply to be _
passed directly to the opened form using DoCmd.OpenForm(). _
Also set .OpenForm() to treat Cancel of the open as NOT an error.
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 = (frmParent Is Nothing)
End Function
'ShowForm opens form strTo and hides the calling form. Returns True on success.
Public Function ShowForm(strTo As String, _
Optional strFilter As String = "", _
Optional varOpenArgs As Variant = Null) As Boolean
ShowForm = True
'Don't even try if caller hasn't initialised Form object yet
If Uninitialised() Then
ShowForm = False
Call ShowMsg(strMsg:=conUnInitMsg, strTitle:="classForm.ShowForm")
Exit Function
End If
Call DoCmd.Restore
'Handle error on OpenForm() only.
On Error GoTo ErrorSF
Call DoCmd.OpenForm(FormName:=strTo, _
WhereCondition:=strFilter, _
OpenArgs:=varOpenArgs)
On Error GoTo 0
Set frmTo = Forms(strTo)
frmFrom.Visible = False
Exit Function
ErrorSF:
ShowForm = False
' If open is cancelled (either by user or code) then simply exit
If Err.Number <> 2501 Then _
Call ErrorHandler(strName:=strTo, _
strFrom:=frmFrom.Name & ".ShowForm", _
lngErrNo:=Err.Number, _
strDesc:=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
'******************************************************************************
调用和使用代码
Option Compare Database
Option Explicit
Private clsTo As New classForm
Private Sub Form_Open(Cancel As Integer)
Set clsTo.frmFrom = Me
End Sub
Private Sub cmdWhatever_Click()
Call clsTo.ShowForm(strTo:="frmWhatever")
End Sub
Private Sub cmdExit_Click()
Call DoCmd.Close
End Sub
Private Sub Form_Close()
'Method must exist in order for container to handle event.
End Sub
第4行指示classForm对象的声明。
第7行指示对象的基本设置。 这告诉类在终止任何被调用的表单后将返回的调用者的形式。
第11行表示正在调用其他表格。 此时,在代码中,类将接管并隐藏调用表单,并显示被调用表单。
第18行到#20行指示事件处理程序的存根,如果该类要能够捕获终止的被调用表单,则该事件处理程序必须存在。 这是班级重新显示呼叫者表格所必需的。
From: https://bytes.com/topic/access/insights/918106-class-module-handle-opening-forms-hierarchically
648

被折叠的 条评论
为什么被折叠?



