我在Access中使用窗体作为对话框,并且试图弄清楚如何以编程方式关闭它。
our-button
在这里不起作用,因为它以DoCmd.Close
开头。
是否有一种方法可以调用用户单击关闭按钮时触发的同一事件? 如果没有,该如何以编程方式关闭表单?
编辑:
为了澄清,这是我的表格:
这是事件:
acDialog
(代码中不包含子代码,只是为了说明问题)
答案 0 :(得分:2)
如果您要查找表单超时,可以研究该函数:
' Opens a modal form in non-dialogue mode to prevent dialogue borders to be displayed
' while simulating dialogue behaviour using Sleep.
' If TimeOut is negative, zero, or missing:
' Form FormName waits forever.
' If TimeOut is positive:
' Form FormName exits after TimeOut milliseconds.
'
' 2018-04-26. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function OpenFormDialog( _
ByVal FormName As String, _
Optional ByVal TimeOut As Long, _
Optional ByVal OpenArgs As Variant = Null) _
As Boolean
Const SecondsPerDay As Single = 86400
Dim LaunchTime As Date
Dim CurrentTime As Date
Dim TimedOut As Boolean
Dim Index As Integer
Dim FormExists As Boolean
' Check that form FormName exists.
For Index = 0 To CurrentProject.AllForms.Count - 1
If CurrentProject.AllForms(Index).Name = FormName Then
FormExists = True
Exit For
End If
Next
If FormExists = True Then
If CurrentProject.AllForms(FormName).IsLoaded = True Then
' Don't reopen the form should it already be loaded.
Else
' Open modal form in non-dialogue mode to prevent dialogue borders to be displayed.
DoCmd.OpenForm FormName, acNormal, , , , acWindowNormal, OpenArgs
End If
' Record launch time and current time with 1/18 second resolution.
LaunchTime = Date + CDate(Timer / SecondsPerDay)
Do While CurrentProject.AllForms(FormName).IsLoaded
' Form FormName is open.
' Bring form to front; it may hide behind a popup form.
DoCmd.SelectObject acForm, FormName
' Make sure form and form actions are rendered.
DoEvents
' Halt Access for 1/20 second.
' This will typically cause a CPU load less than 1%.
' Looping faster will raise CPU load dramatically.
Sleep 50
If TimeOut > 0 Then
' Check for time-out.
CurrentTime = Date + CDate(Timer / SecondsPerDay)
If (CurrentTime - LaunchTime) * SecondsPerDay > TimeOut / 1000 Then
' Time-out reached.
' Close form FormName and exit.
DoCmd.Close acForm, FormName, acSaveNo
TimedOut = True
Exit Do
End If
End If
Loop
' At this point, user or time-out has closed form FormName.
End If
' Return True if the form was not found or was closed by user interaction.
OpenFormDialog = Not TimedOut
End Function
它取自我的项目VBA.ModernBox中的模块 ModernBox.bas 。
答案 1 :(得分:1)
对于“关闭/取消”按钮,请始终使用
DoCmd.Close acForm, Me.Name
即使您重命名表单,也可以使用Me.Name
。
您可能要添加
DoCmd.Close acForm, Me.Name, acSaveNo
避免用户设置的过滤器或排序顺序与表单一起保存。