Problems closing multiple-instance forms

DataMinerHome

Registered User.
Local time
Today, 08:44
Joined
Jan 4, 2010
Messages
57
HI, I am using Allen Browne's method for handling multiple instances of a form. See http://allenbrowne.com/ser-35.html for code and details. Using
Access 2010. However, I am having a problem where sometimes the instance does not close properly.
I've posted my entire FormMultipleInstance module, mostly copied directly from Allen, below.

To Open the form instance I use
Set Frm = GetNewForm(fname)
'Append it to our collection.
clnClient.Add Item:=Frm, Key:=CStr(Frm.Hwnd)

My form's On_close event contains the following code:
RemoveInstance.Me

The form has a button "ClickClose" with the folling code:
Form_Close

Now, here's what I don't understand:
If I click the ClickClose button, RemoveInstance runs, and changes clnClient.count from 1 to 0. At this point, the instance is still visible on screen.
Then apparently the form's close event fires, so RemoveInstance runs a SECOND time, with clnClient.count already at 0, and the instance then actually closes.
Hmmm... don't really understand why it works this way, but it works...
HOWEVER, If I run a print preview of the instance, close the print priview and then click ClickClose, the instance does not close. RemoveInstance runs,
but clnClient.count starts a 1 and stays at 1.
What am I doing wrong????
Thanks for any help you can offer.

Here is my entire FormMultipleInstance module
Option Compare Database
Option Explicit
'Copied this code from http://allenbrowne.com/ser-35.
Public clnClient As New Collection 'Instances of frmClient.
Function OpenAClient(FormName As String)
'Purpose: Open an independent instance of form frmClient.
Dim Frm As Form

'Open a new instance, show it, and set a caption.
'Set frm = New Form_frmClient
Set Frm = GetNewForm(FormName)



Frm.Visible = True
' frm.Caption = frm.Hwnd & ", opened " & Now()
'Append it to our collection.
clnClient.Add Item:=Frm, Key:=CStr(Frm.Hwnd)
Set Frm = Nothing
End Function
Function CloseAllClients()
'Purpose: Close all instances in the clnClient collection.
'Note: Leaves the copy opened directly from database window/nav pane.
Dim lngKt As Long
Dim lngI As Long
lngKt = clnClient.Count
For lngI = 1 To lngKt
clnClient.Remove 1
Next
End Function
Function GetNewForm(fType As String) As Form
Dim Frm As Form
'this is necessary so that multiple instances of the form can be opened.
Select Case fType
Case "MC1"
Set Frm = New Form_MC1
Case "MC2"
Set Frm = New Form_MC2
Case "MC3"
Set Frm = New Form_MC3
Case "MC4"
Set Frm = New Form_MC4
Case "MC6"
Set Frm = New Form_MC6
Case "MC7"
Set Frm = New Form_MC7
Case "MC10"
Set Frm = New Form_MC10
Case "MC11"
Set Frm = New Form_MC11
Case "MC13"
Set Frm = New Form_mc13
Case "MC14"
Set Frm = New Form_MC14
Case "MC15"
Set Frm = New Form_MC15
Case "MC16"
Set Frm = New Form_MC16
Case "MC17"
Set Frm = New Form_MC17
Case "MC18"
Set Frm = New Form_MC18
Case "MC19"
Set Frm = New Form_MC19
Case "MC21"
Set Frm = New Form_MC21
Case "MC22"
Set Frm = New Form_MC22
Case "MC23"
Set Frm = New Form_MC23
Case "MC24"
Set Frm = New Form_MC24
Case "MC25"
Set Frm = New Form_mc25
Case "MC26"
Set Frm = New Form_MC26
Case "MC27"
Set Frm = New Form_MC27


Case Else
MsgBox "Form " & fType & " is not available": Exit Function

Set Frm = Nothing

End Select
'frm.Visible = False
Frm.Visible = Not Frm Is Nothing
'other code here
Set GetNewForm = Frm
End Function

Public Sub RemoveInstance(F As Form)
'Purpose: Remove this instance from clnClient collection.
Dim obj As Object 'Object in clnClient
Dim blnRemove As Boolean 'Flag to remove it.
On Error Resume Next
' added 6/3/11
If clnClient.Count = 0 Then
DoCmd.Close acForm, F.Name, acSavePrompt
Exit Sub
End If
'Check if this instance is in the collection.
For Each obj In clnClient
If obj.Hwnd = F.Hwnd Then
blnRemove = True
Exit For
End If
Next
'Deassign the object and remove from collection.
Set obj = Nothing
If blnRemove Then
clnClient.Remove CStr(F.Hwnd)


End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom