Custom event does not fire

joeKra

Registered User.
Local time
Today, 09:34
Joined
Jan 24, 2012
Messages
208
Hi Guys

Ihave implemented custom events to sync between open forms.
I noticed that if the code is engaging in middle of the way referncing a form with form constant (Form_formName) then the listeners stops listening so the event isn't firing, without getting the reason of this i just avoid using it :)
Now, i have a problem that i want to open multiple instances of the same form and i didn't find another way than set frm = new form_formName as this is the only way i seem to be able to reference a closed form, so my question as followes
1. whether the "breaking" issue can be solved without avoiding "Form_FormName"
2. or a way to open\reference a form without using "form_FormName"

Thanks in advance !
 
Let's say you have FormA with a custom event as follows . . .
Code:
Public Event SomethingHappened(Sender as Form_FormA)
In order to handle that event on FormB, you must declare a WithEvents variable, and an event handler . . .
Code:
[COLOR="Green"]'FormB variable declarations[/COLOR]
Private WithEvents m_formA As Form_FormA

[COLOR="Green"]'FormB event handler for the FormA SomethingHanppend event[/COLOR]
Private Sub m_formA_SomethingHappened(frmA As Form_FormA)
   MsgBox "Something happened on " &  Typename(frmA)
End Sub
. . . AND, you need to set m_formA to point to a valid FormA instance. A copule of options here are, check for FormA in Forms when FormB opens . . .
Code:
Private Sub Form_Open(Cancel As Integer)
[COLOR="Green"]'   this is FormB's Open event handler[/COLOR]
    If CurrentProject.AllForms("FormA").IsLoaded Then
[COLOR="Green"]        'we check if formA is already open, and if so retain a reference to it[/COLOR]
        set m_formA = Forms("FormA")
    End If
End Sub

Another little trick you can do is expose FormA as a custom property of FormB, so that any reference creates a FormA, and assigns it to the variable. Consider this code . . .
Code:
Public Property Get FormA as Form_FormA
[COLOR="Green"]'This property always returns an instance of FormA
'even if FormA wasn't open at the time the call was made[/COLOR]
[COLOR="Green"]   'check if we already have a valid reference[/COLOR]
   if m_formA is nothing then
[COLOR="Green"]      'if we don't, create one . . .[/COLOR]
      docmd.openform "FormA"
[COLOR="Green"]      'and assign it to our local withevents variable[/COLOR]
      set m_formA = Forms("FormA")
   end if
[COLOR="Green"]   'always returns a valid FormA[/COLOR]
   set FormA = m_formA
end property
And, you can use a Public Property Set on FormB to receive a reference to FormA from any consumer, like . . .
Code:
Public Property Set FormA(frm as Form_FormA)
[COLOR="Green"]'   As a public property of FormB, any other consumer can now set the 
'   FormA reference on FormB
[/COLOR]   set m_formA = frm
End Property
. . . and if you do it that way, FormA can open FormB, and set itself as FormB's FormA property, so imagine this code on FormA . . .
Code:
private sub cmdOpenFormB_Click()
[COLOR="Green"]'   Opens FormB from a button click on FormA[/COLOR]
    dim frmB as Form_FormB   [COLOR="Green"]'FormB strongly typed variable[/COLOR]
    docmd.openform "FormB"   [COLOR="Green"]'Open FormB here[/COLOR]
    set frmB = Forms("FormB")     [COLOR="Green"]'save object reference to FormB[/COLOR]
    set frmB.FormA = Me      [COLOR="Green"]'set the FormA property of FormB[/COLOR]
end sub
Does that make sense?
 

Users who are viewing this thread

Back
Top Bottom