Hi all, I have recently added code to allow a pop up alarm form (frmPopUpAlarm) to have multiple instances so more than one alarm can open at once.
With some help from this wonderful forum I got it working here
However I now have strange behavior with one of the forms buttons to hide the form, when more than one instance is open, and I can't work out where its going wrong.
A command button on the form hides the form for a chosen number of minutes using an unbound text box, then on the forms timer the form becomes visible again. This is to 'dismiss' the form so the user can finish what they're doing as the form is modal. Heres the code:-
So this worked fine before adding the multiple instances code, and still works fine now, but only when one 'frmPopUpAlarm' form is opened. If two instances of the form are open, I can set the number of minutes and 'dismiss' (hide) both forms. However... only the first form I dismissed reappears after the set number of minutes. The other form does not become visible.
I can't work out where its going wrong
Here is the rest of the code for multiple instances of the form....
The main function in a module
The 'startup' form timer code which Calls the function and opens the frmPopUpAlarm'...
And the code in the Close event of frmPopUpAlarm which removes the instance of the form when the form is closed...
I thought that since the form was an 'instance', separate from others, that its timer event etc would work independantly and not be affected, but it appears something else is happening??

With some help from this wonderful forum I got it working here
However I now have strange behavior with one of the forms buttons to hide the form, when more than one instance is open, and I can't work out where its going wrong.
A command button on the form hides the form for a chosen number of minutes using an unbound text box, then on the forms timer the form becomes visible again. This is to 'dismiss' the form so the user can finish what they're doing as the form is modal. Heres the code:-
Code:
Private Sub Form_Timer()
Me.Visible = True
Me.TimerInterval = 0
End Sub
Private Sub btnDismiss_Click()
If Nz(Me.txtDismissMins, "") <> "" Then
Me.TimerInterval = Me.txtDismissMins * 60000
Me.Visible = False
End If
End Sub
So this worked fine before adding the multiple instances code, and still works fine now, but only when one 'frmPopUpAlarm' form is opened. If two instances of the form are open, I can set the number of minutes and 'dismiss' (hide) both forms. However... only the first form I dismissed reappears after the set number of minutes. The other form does not become visible.
I can't work out where its going wrong
Here is the rest of the code for multiple instances of the form....
The main function in a module
Code:
Option Compare Database
Option Explicit
Public clnPopUpAlarm As New Collection 'Instances of frmPopUpAlarm
Function OpenAnAlarm(ByVal strFilter As String)
'Purpose: Open an independent instance of form frmPopUpAlarm
Dim frm As Form
'Open a new instance, show it, and set a caption.
Set frm = New Form_frmPopUpAlarm
frm.Visible = True
frm.Caption = frm.hWnd & ", opened " & Now()
'Append it to our collection.
clnPopUpAlarm.Add Item:=frm, Key:=CStr(frm.hWnd)
frm.Filter = strFilter
frm.FilterOn = True
Set frm = Nothing
End Function
The 'startup' form timer code which Calls the function and opens the frmPopUpAlarm'...
Code:
Private Sub Form_Open(Cancel As Integer)
Me.TimerInterval = 60000
End Sub
Private Sub Form_Timer()
Dim strWhere As String
Dim varCommentNumber As Variant
strWhere = "Format([CommentAlarm], ""yyyymmddhhnn"") = '" & _
Format(Now(), "yyyymmddhhnn") & "' AND " & _
"[AlarmComputerName] = '" & Environ("Computername") & "'"
varCommentNumber = (DLookup("[CommentNumber]", "tblCustomerComments", strWhere))
If IsNull(varCommentNumber) Then
' There's no current comment for that machine
Else
Call OpenAnAlarm("[CommentNumber] = " & varCommentNumber)
End If
End Sub
And the code in the Close event of frmPopUpAlarm which removes the instance of the form when the form is closed...
Code:
Private Sub Form_Close()
'Purpose: Remove this instance from the clnPopUpAlarm collection.
Dim obj As Object 'Object in clnPopUpAlarm
Dim blnRemove As Boolean 'Flag to remove it.
'Check if this instance is in the collection.
' (It won't be if form was opened directly, or code was reset.)
For Each obj In clnPopUpAlarm
If obj.hWnd = Me.hWnd Then
blnRemove = True
Exit For
End If
Next
'Deassign the object before removing from collection.
Set obj = Nothing
If blnRemove Then
clnPopUpAlarm.Remove CStr(Me.hWnd)
End If
End Sub
I thought that since the form was an 'instance', separate from others, that its timer event etc would work independantly and not be affected, but it appears something else is happening??