Capture event from child form (not sub form)

hbrems

has no clue...
Local time
Today, 02:26
Joined
Nov 2, 2006
Messages
181
Is there any way to capture an event of a child form? The child form is a separate form, thus not a subform. Something like this:

Code:
Private WithEvents child As Form

Private Sub child_OnUnload()
     MsgBox "Child closing"
End Sub
 
I think it would just be ...
Code:
private withevents child as form

private sub Form_Open(Cancel As Integer)
[COLOR="Green"]  'set the withevents variable to a valid object[/COLOR]
  docmd.openform "somechildform"
  set child = forms("somechildform")
end sub

Private Sub child_Unload(Cancel As Integer)
  MsgBox "Child Unloaded"
End Sub
But yes, that is totally doable. You can even create objects that raise their own custom events. You are not limited to using only those that are already raised by existing objects types.
Cheers,
Mark
 
Well it doesn't seem to work for forms. All events in my custom classes work just fine. But when I try to capture an event from a child form it doesn't seem to trigger.

Code:
Private WithEvents pChild As Form

Private Sub btnNew_Click()
    Set child = New Form_frmTrialNew
    child.Visible = True
End Sub

Private Sub pChild_Unload(Cancel As Integer)
    MsgBox "Bye bye"
End Sub
 
If you compare your code with lagbolt's code, it's not the same in context.
 
lagbolt:
Code:
private sub Form_Open(Cancel As Integer)
  'set the withevents variable to a valid object
[COLOR=Red]  docmd.openform "somechildform"
  set child = forms("somechildform")[/COLOR]
end sub

You:
Code:
Private Sub btnNew_Click()
[COLOR=Red]    Set child = New Form_frmTrialNew
    child.Visible = True[/COLOR]
End Sub
Is there any reason why you're creating a new instance of the form instead of opening it? Also, pChild is not set to the form instance.
 
pChild is not set to the form instance.

That's just a typo. It should be:

Code:
Private Sub btnNew_Click()
    Set pChild = New Form_frmTrialNew
    pChild.Visible = True
End Sub

The reason why I'm creating an instance is because I might want to have multiple instances of a form open at one time.
 
The event won't fire without the property set:
Code:
Private WithEvents pChild As Form

Private Sub btnNew_Click()
    Set pChild = New Form_frmTrialNew
    pChild.Visible = True

[COLOR=Red]    If pChild.OnUnLoad <> "[Event Procedure]" Then
        pChild.OnUnload = "[Event Procedure]"
    End If[/COLOR]
End Sub

Private Sub pChild_Unload(Cancel As Integer)
    MsgBox "Bye bye"
End Sub
 
Sweet, it works now. Thank you so much, this will save me a lot of trouble in the future.
 

Users who are viewing this thread

Back
Top Bottom