Solved Main Form not Picking up Custom Events on Sub Form (1 Viewer)

Fran Lombard

Registered User.
Local time
Today, 18:11
Joined
Mar 12, 2014
Messages
132
Having a vexing problem and was hoping for some insite as to where to look.

I have a form divided into 6 quadrants, each quadrend is comprised of a sub form. Each subform contains a form that has 1 sub form. At each level i declare (WithEvents) and set object variable that reference the subforms/forms below them.

The communication between the forms are either up or down through theese objects using either Public subs in lower levels to talk down the line and public events to talk up the chain.

All communication lines work perfectly except 1 of the quadrants. In this quadrent, the lowest level custom form event is being passed up to the middle teir to its lisener, which contains inside the lisener, the RaiseEvent to pass on up. The problem is the top forms lisener never fires.

This exact configuration is working as expected in all other quadrants. Ive checked and double and triple checked all declarations, scoping and instansiations with no joy.

Any ideas?

Thanks
Fran
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:11
Joined
Jul 9, 2003
Messages
16,271
The problem is the top forms lisener never fires.

You mentioned passing information up and down through a series of forms/sub-forms. If this is effective and working, then my observation is probably wrong. Have you considered that a subform isn't a child of a form, a child of the parent form? If you write code to find the parent of a subform expecting the main form to be the parent for instance, then your code will fail because the parent of the subform is actually a subform/subreport control, NOT a Form as you would expect.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:11
Joined
May 21, 2018
Messages
8,525
That is hard to decipher without seeing it because you have a chain of things that have to happen. Can you verify that the custom event is raised in the middle subform? However, I think If all events are trapped in the main form I would declare the sub-subforms with events and by pass the custom raise event in the subform. Can you post a stripped version of this?
 

Fran Lombard

Registered User.
Local time
Today, 18:11
Joined
Mar 12, 2014
Messages
132
OK Here's the Code:


Top Form (Main) - Object Declarations and Instantiation

Code:
'-----------------------------------------------------------------------------------
'         Upper Right Section Properties and Objects
'-----------------------------------------------------------------------------------
Dim myGroupMemberDefinitionSub As SubForm
Dim WithEvents myGroupMemberDefinitionForm As Form_ActivityGroupStub


Private Sub Form_Load()
On Error GoTo SubError
Application.Echo False
DoCmd.Hourglass True
DoCmd.Minimize


'-------------------------------------------------------------------------
'            INITIALIZE UPPER RIGHT SECTION (PARENT GROUP) - (BEGIN)
'-------------------------------------------------------------------------
'Initialize Child SubForm Objects
'-------------------------------------------------------------------------
Set myGroupMemberDefinitionSub = Me.subGroupMemberDefinition
myGroupMemberDefinitionSub.HorizontalAnchor = acHorizontalAnchorBoth
myGroupMemberDefinitionSub.VerticalAnchor = acVerticalAnchorBoth

'-------------------------------------------------------------------------
'Set Source Object and Link Master and Child Fields
'-------------------------------------------------------------------------
myGroupMemberDefinitionSub.SourceObject = "ActivityGroupStub"
myGroupMemberDefinitionSub.LinkMasterFields = "txtGroupID"
myGroupMemberDefinitionSub.LinkChildFields = "ID"
'-------------------------------------------------------------------------
'Initialize Form object for Form inside of Member Subledger Object
'-------------------------------------------------------------------------
Set myGroupMemberDefinitionForm = myGroupMemberDefinitionSub.Form
myGroupMemberDefinitionForm.RecordSelectors = False
myGroupMemberDefinitionForm.NavigationButtons = False

'-------------------------------------------------------------------------
'            INITIALIZE UPPER RIGHT SECTION (CHILD OR MEMBER) - (END)
'-------------------------------------------------------------------------

SubExit:
    DoCmd.Maximize
    DoCmd.Hourglass False
    Application.Echo True
    Exit Sub
SubError:
    MsgBox Err.Description
    Resume SubExit
End Sub


Top Tier Event Listeners

Code:
'-------------------------------------------------------------------------------------------
'*********   UPPER RIGHT CAPTURED EVENTS  (BEGIN)     **************************************
'-------------------------------------------------------------------------------------------

Private Sub myGroupMemberDefinitionForm_ButtonSaveClicked()
    MsgBox "Botton Save"
End Sub

Private Sub myGroupMemberDefinitionForm_LatestID(CurrentID As Long)
    MsgBox "Latest ID"
End Sub

Private Sub myGroupMemberDefinitionForm_NewGroupClicked()
    MsgBox "New Group"
End Sub

Private Sub myGroupMemberDefinitionForm_NewItemSelected(LaborActivityID As Long)
    MsgBox "New Item"
End Sub

Private Sub myGroupMemberDefinitionForm_ReFreshMemberShipClicked()
    MsgBox "Refresh Membership"
End Sub

'-------------------------------------------------------------------------------------------
'*********   UPPER RIGHT CAPTURED EVENTS  (END)     **************************************
'-------------------------------------------------------------------------------------------



Middle Tier Object Declarations and Instantiation


Code:
'-----------------------------------------------------------------------------------------
'       PUBLIC EVENTS - TALK TO MY PARENT
'-----------------------------------------------------------------------------------------
Public Event NewGroupClicked()
Public Event ButtonSaveClicked()
Public Event ReFreshMemberShipClicked()
Public Event LatestID(CurrentID As Long)
Public Event NewItemSelected(LaborActivityID As Long)

'-----------------------------------------------------------------------------------------
'       SUBFORM CONTAINER Object Variable and Constants
'-----------------------------------------------------------------------------------------
Dim mySubForm As SubForm

Private Const mySubForm_SourceObject As String = "ActivityGroupActivityDS2"
Private Const mySubForm_LinkMasterFields As String = "ID"
Private Const mySubForm_LinkChildFields As String = "ActivityGroupID"



'-----------------------------------------------------------------------------------------
'       SUBFORM'S FORM Object Variable and Constants
'-----------------------------------------------------------------------------------------
Dim WithEvents myActivityGroupSubFormForm As Form_ActivityGroupActivityDS2

Private Const myActivityGroupSubFormForm_NavigationButtons As Boolean = False
Private Const myActivityGroupSubFormForm_RecordSelectors As Boolean = False

Private Sub Form_Load()

    Set mySubForm = Me.subGroupMembers
    mySubForm.SourceObject = mySubForm_SourceObject
    mySubForm.LinkMasterFields = mySubForm_LinkMasterFields
    mySubForm.LinkChildFields = mySubForm_LinkChildFields
    
    
    Set myActivityGroupSubFormForm = New Form_ActivityGroupActivityDS2
    Set myActivityGroupSubFormForm = mySubForm.Form
    myActivityGroupSubFormForm.NavigationButtons = False
    myActivityGroupSubFormForm.RecordSelectors = False

End Sub


Middle Tier Raise Events - Sending UP


Code:
Private Sub Form_Current()
    RefreshGroups
    RaiseEvent LatestID(Nz(Me.ID, 0))
End Sub

Private Sub Form_BeforeInsert(Cancel As Integer)
    If Me.NewRecord Then
        Me.AttachID = 0
        Me.ImageID = 0
    End If
End Sub

Private Sub cmdNewGroup_Click()
    Me.DataEntry = True
    RaiseEvent NewGroupClicked
End Sub

Private Sub cmdSave_Click()
    SaveRecord
    RefreshGroups
    RaiseEvent ButtonSaveClicked
End Sub

Private Sub cmdRefreshMembership_Click()
    
    RefreshGroups
    RaiseEvent ReFreshMemberShipClicked
End Sub

Public Sub RefreshGroups()
    myActivityGroupSubFormForm.UpdateFields
    RaiseEvent ReFreshMemberShipClicked
    
End Sub


Mid Tier Listener



Code:
'--------------------------------------------------------------------------------------------------------------------
'               Middle Tier Listener - This tiers RaiseEvent to Pass on Up
'--------------------------------------------------------------------------------------------------------------------
Private Sub myActivityGroupSubFormForm_RowSelected(ActivityGroup As Long, LaborActivityID As Long)
    RaiseEvent NewItemSelected(LaborActivityID)
End Sub


Lowest Level Tier Entire Module


Code:
Option Compare Database
Option Explicit

Public Event RowSelected(ActivityGroup As Long, LaborActivityID As Long)

Private Sub Form_Current()
    RaiseEvent RowSelected(Nz(Me.ActivityGroupID, 0), Nz(Me.LaborActivityID, 0))
End Sub

Public Sub UpdateFields()
    Me.RecordSource = Me.RecordSource
    Me.ActivityGroupID.Requery
    Me.LaborActivityID.Requery
End Sub



Well thats the journey up and down the bunny trail.
The middle tier picks up the events from the lowest and seems to step through the RaiseEvents step.
Stepping through the code takes me right through these but it never jumps back up to the top tier.

Whats crazy - the VBA editor recognize the Object and I used it to insert the Event procedure into the top tier so I know my top level is recognizing
this object.

I'm at a loss and spent over 6 hours banging my head. Any help would be appreciated.
Thanks
Fran
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:11
Joined
May 21, 2018
Messages
8,525
Can you not just trap the event in the main form, by declaring the sub sub form with events. Not that is your problem, but it takes a pass through out of the process.

Maybe you done this already
but can you put a msgbox here

Code:
Private Sub Form_Current()
    RaiseEvent RowSelected(Nz(Me.ActivityGroupID, 0), Nz(Me.LaborActivityID, 0))
    Msgbox "Current in sub subform"
End Sub

and here

Code:
Private Sub myActivityGroupSubFormForm_RowSelected(ActivityGroup As Long, LaborActivityID As Long)
    RaiseEvent NewItemSelected(LaborActivityID)
    msbox "Raised in middletier"
End Sub
Just to ensure these are being fired and trapped
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:11
Joined
May 21, 2018
Messages
8,525
One very simple error that can get you, is that you have to ensure that in these forms if you have an event you have the word [Event Procedure] in the procedure property. Normally not a problem when working with a form because usually you hit the elipses to make the procedure. But this is more likely mistake in trapping other form events. So in your sub even though you have this
Code:
Private Sub Form_Current()
    RaiseEvent RowSelected(Nz(Me.ActivityGroupID, 0), Nz(Me.LaborActivityID, 0))
    Msgbox "Current in sub subform"
End Sub

If you forgot [event procedure], the event is not announced.
 

Fran Lombard

Registered User.
Local time
Today, 18:11
Joined
Mar 12, 2014
Messages
132
One very simple error that can get you, is that you have to ensure that in these forms if you have an event you have the word [Event Procedure] in the procedure property. Normally not a problem when working with a form because usually you hit the elipses to make the procedure. But this is more likely mistake in trapping other form events. So in your sub even though you have this
Code:
Private Sub Form_Current()
    RaiseEvent RowSelected(Nz(Me.ActivityGroupID, 0), Nz(Me.LaborActivityID, 0))
    Msgbox "Current in sub subform"
End Sub

If you forgot [event procedure], the event is not announced.
Maj thanks for your respone - im not sure what you mean by[Event Procedure] am i missing some key syntax?

By the way - those calks to the msgbox was just for testing - the real to implement the work was coming once i knew the plumbing was working.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:11
Joined
May 21, 2018
Messages
8,525
im not sure what you mean by[Event Procedure] am i missing some key syntax?
Normally when you create an event on a form, you go the the event property and pull down the ... ellipses and it creates an event handler. It also puts the words "[Event Procedure]" in the event property ( or [embedded Macro] or SomeFunction()). That is needed for the event to announce itself. I just know when I trap other events in other forms, I may forget this because I am not navigating through the form, and simply build an event handler without going through the form and ensuring I have this in the event property.
those calks to the msgbox was just for testing
That was my question. If you already had put msgboxes to verify the lowest level and middle events were being raised.

Can you try changing this to just form.
Dim WithEvents myActivityGroupSubFormForm As Access.form
Leave the set as is.

Like I said, chasing these down without the actual code is hard. Can you post a small demo?
As far as I can tell it all looks good. I am guessing it is something simple, but just not looking at the right place.
 
Last edited:

Fran Lombard

Registered User.
Local time
Today, 18:11
Joined
Mar 12, 2014
Messages
132
Normally when you create an event on a form, you go the the event property and pull down the ... ellipses and it creates an event handler. It also puts the words "[Event Procedure]" in the event property ( or [embedded Macro] or SomeFunction()). That is needed for the event to announce itself. I just know when I trap other events in other forms, I may forget this because I am not navigating through the form, and simply build an event handler without going through the form and ensuring I have this in the event property.

That was my question. If you already had put msgboxes to verify the lowest level and middle events were being raised.

Can you try changing this to just form.
Dim WithEvents myActivityGroupSubFormForm As Access.form
Leave the set as is.

Like I said, chasing these down without the actual code is hard. Can you post a small demo?
As far as I can tell it all looks good. I am guessing it is something simple, but just not looking at the right place.
Thanks again for your effots.
Dim as Access.Form did not help.

I did however drop the offending sub on another test form and copied and pasted the plumbing and it worked as expected.

This tells me there must be something happening when its contained in my big main form. Some naming overlap, having more then 1 sub based on the same underlying table. Some confliict in the inter-play between the different elements.

At lleast i have a place to look.
 

Fran Lombard

Registered User.
Local time
Today, 18:11
Joined
Mar 12, 2014
Messages
132
Turned out one of my setting of a subforms LinkMasterField was screwed up and cuased improper initialization.

Thanks guys for your efforts
Fran
 

Users who are viewing this thread

Top Bottom