最简单的方法是简单地以对话模式打开表单。例如,
DoCmd.OpenForm nm, acNormal, , whr, mode, acDialog, args
这将暂停调用模块中代码的执行,直到表单关闭。它还将阻止用户与任何其他表单交互,直到“对话框”表单关闭。
我发现在对话框模式下打开绑定表单,更新数据,关闭表单,然后刷新调用表单上的对象(例如,组合框的行源)并不总是可靠的。
下面是我编写的一个通用函数,用于“暂停”调用代码,而无需在对话框模式下打开表单,也不会显着影响用户界面的性能。它适用于表单和报告。请注意,睡眠 API 声明必须位于代码模块的顶部(在模块声明部分)。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'-------------------------------------------------------------------------------
' Procedure : WaitTilObjClosed
' Author : Mike
' Date : 1/7/2009
' Purpose : Halts program execution until user closes object. User is
' generally unaffected by the loop.
' Requires : Sleep API sub
' Notes - while Sleeping other programs can use processor but access cannot;
' - during DoEvents, other parts of Access can use processor;
' - without the Sleep call, processor usage stays at 100% for MSACCESS.EXE
' - with a long Sleep call, Access becomes noticeably sluggish
'-------------------------------------------------------------------------------
'
Sub WaitTilObjClosed(ObjType As AcObjectType, ObjName As String)
Do
DoEvents
Sleep 1
If (SysCmd(acSysCmdGetObjectState, ObjType, ObjName) = 0) Then Exit Do
Loop
End Sub
您可以按如下方式使用它:
DoCmd.OpenForm "MyForm"
WaitTilObjClosed acForm, "MyForm"
MsgBox "MyForm is now closed"
DoCmd.OpenReport "MyReport", acViewPreview
WaitTilObjClosed acReport, "MyReport"
MsgBox "MyReport is now closed"