带WithEvents的控件包装类执行DoCmd.Close致Access崩溃求助
Access执行DoCmd.Close时硬崩溃的排查分析
触发崩溃的命令:
DoCmd.Close acForm, lstrParentName, acSaveNo
该命令在组合框包装类(声明了WithEvents的组合框变量赋值的类模块)中执行时,会持续导致Access硬崩溃,目前无法定位原因。
上下文信息
- 表单绑定到表单包装类
clsFrm,该类将控件绑定到对应类型的控件包装类(例如组合框对应clsCbo) clsCbo负责处理绑定组合框的各类事件,包括OnDblClick事件clsCbo还绑定了一个二级组合框包装类,用于封装相关功能代码clsCbo的mCbo_DblClick()方法会调用导航过程nSuper(),该过程会打开另一个表单,并通过上述DoCmd.Close命令关闭当前表单,此操作会直接引发Access硬崩溃/异常退出
异常现象
- 导航过程
nSuper()能无错误执行至退出步骤 - 源表单关闭时,其关联的所有表单和控件包装类都正常执行了
Class_Terminate()过程,无报错 - 目标表单的包装类完成绑定逻辑,子表单开始加载
- 手动点击窗口右上角的“X”关闭表单时,无任何异常
关键测试结果
- 注释掉
DoCmd.Close命令后,程序运行完全正常 - 跳过绑定二级组合框包装类的步骤后,也不会出现崩溃问题
请问可能的原因是什么?
nSuper()过程代码
Private Sub nSuper() ' Method to navigate from cboSuper~ID to the entity form. ' Dependencies: ' - mstrPath declared in Class_Initialize() ' - stdObjectTests.IsSubform() ' Dependants: ' - mCbo_DblClick On Error GoTo ErrorHandler 200 Dim cboMe As ComboBox 210 Dim frmMe As Access.Form 220 Dim intPK As Long 230 Dim strDest As String 240 Dim strEnt As String 250 Dim strFld As String 260 Dim strPK As String 270 Dim strRS As String 280 Dim strWHERE As String ' Derive the filter criterion from the control: 290 Set cboMe = mCbo 310 intPK = cboMe.Value ' Derive Entity from Form.RecordSource: 320 Set frmMe = mParent 350 strRS = frmMe.RecordSource 370 strEnt = Right(strRS, Len(strRS) - 3) ' Compose the destination Form.Name from Entity: 400 If strEnt = "Party" Then 410 strDest = "frmFirm" 420 Else 430 strDest = "frm" & strEnt 440 End If ' Compose the filter: 460 strPK = strEnt & "ID" 480 strFld = "tbl" & strEnt & "." & strPK 500 strWHERE = strFld & " = " & intPK 510 If strDest = frmMe.Name Then ' Re-filter the current form if it is the entity form: 520 frmMe.Filter = strWHERE 530 frmMe.FilterOn = True 540 Else ' Otherwise, open the entity form: 550 DoCmd.OpenForm strDest, acNormal, , strWHERE ' Close the current form or its parent: ' - Either may crash Access: 560 If IsSubform(frmMe) Then 570 DoCmd.Close acForm, frmMe.Parent.Name, acSaveNo 580 Else 590 DoCmd.Close acForm, frmMe.Name, acSaveNo 600 End If 610 End If ExitProcedure: 913 Close lintFF 914 Set lFso = Nothing Set cboMe = Nothing Set frmMe = Nothing Exit Sub ErrorHandler: 890 Resume ExitProcedure End Sub 'nSuper()
内容的提问来源于stack exchange,提问作者ebsf
相关产品推荐
相关产品推荐

