Reputation: 2076
I'm using a form as a dialog in Access, and I'm trying to figure out how to close it programmatically.
DoCmd.Close
doesn't work here because it's opened as acDialog
.
Is there a way to call the same event that gets triggered when a user clicks the close button? If not, how can I close the form programmatically?
Edit:
To clarify, this is my form:
And this is the event:
Private Sub btnCancel_Click()
'DOES NOT WORK:
DoCmd.Close acForm, "Form_Authentication Required"
End Sub
Private Sub btnCancel_Click()
'DOES NOT WORK EITHER:
DoCmd.Close acDialog, "Form_Authentication Required"
End Sub
(The subs are not both in the code, they are just to illustrate the question)
Upvotes: 0
Views: 250
Reputation: 27634
For a Close/Cancel button, always use
DoCmd.Close acForm, Me.Name
Using Me.Name
will always work, even if you rename the form.
You may want to add
DoCmd.Close acForm, Me.Name, acSaveNo
to avoid filters or sort order that the user has set, to be saved with the form.
Upvotes: 1
Reputation: 55841
If you are looking for timeout of the form, you can study the function:
' 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
It is taken from module ModernBox.bas from my project VBA.ModernBox.
Upvotes: 2