riktek
Member
- Local time
- Today, 14:55
- Joined
- Dec 15, 2023
- Messages
- 124
A few of us were putting heads together the other day to resolve some difficulties around closing form instances and I thought I'd report back.
What's new recently in the experience of several of us is that apparently, clearing a reference to a form instance is no longer sufficient to close that instance, although it remains necessary. The default behavior, as noted by Allen Browne, had been that removing references to a form instance closes it.
So, apparently, one now must also close the form instance expressly. This requires a DoCmd.Close call but this poses a few difficulties:
- DoCmd.Close only permits object identification by name.
- Each instance of a form has the same name.
- When provided a name, DoCmd.Close operates on the first member of Application.Forms with that name, not the last. This poses no difficulty when closing all instances so long as one gets the number right. One can't specify a specific instance or subset of instances, however, so calling DoCmd.Close, even from the form's module, most likely will close another, or an unintended, instance.
The solution lies in the fact that all DoCmd.Close arguments are optional. If omitted (except perhaps for the Save parameter), the command operates on the active window, but note:
- A form's window does not become active simply because code is executing in its module.
- So, we first must set focus to the form instance intended to be closed.
What we arrived at is:
This is intended to be callable either (a) without an argument as a private function in a form module; or (b) with an argument as a public function in a standard module.
Of course, we may have gotten something wrong, so comments and corrections are welcome.
What's new recently in the experience of several of us is that apparently, clearing a reference to a form instance is no longer sufficient to close that instance, although it remains necessary. The default behavior, as noted by Allen Browne, had been that removing references to a form instance closes it.
So, apparently, one now must also close the form instance expressly. This requires a DoCmd.Close call but this poses a few difficulties:
- DoCmd.Close only permits object identification by name.
- Each instance of a form has the same name.
- When provided a name, DoCmd.Close operates on the first member of Application.Forms with that name, not the last. This poses no difficulty when closing all instances so long as one gets the number right. One can't specify a specific instance or subset of instances, however, so calling DoCmd.Close, even from the form's module, most likely will close another, or an unintended, instance.
The solution lies in the fact that all DoCmd.Close arguments are optional. If omitted (except perhaps for the Save parameter), the command operates on the active window, but note:
- A form's window does not become active simply because code is executing in its module.
- So, we first must set focus to the form instance intended to be closed.
What we arrived at is:
Code:
Function CloseMe(Optional ByRef objMe As Object) As Boolean
Dim blnReset As Boolean
If (objMe Is Nothing) Then
blnReset = True
Set objMe = Access.Application.CodeContextObject
End If
objMe.SetFocus
DoCmd.Close , , acSaveNo
If blnReset Then Set objMe = Nothing
CloseMe = (Err.Number = 0&)
End Function 'CloseMe()
This is intended to be callable either (a) without an argument as a private function in a form module; or (b) with an argument as a public function in a standard module.
Of course, we may have gotten something wrong, so comments and corrections are welcome.
Last edited: