Link to home
Start Free TrialLog in
Avatar of JeffreyWest
JeffreyWest

asked on

Raising an Event from a subform

What's the trick to raise an event from a subform? I'm trying to open a secondary form after a button click on a subform. The event should pass an index number to the newly opened secondary form. The form is opening, but there's no index number. I've added the same code to a button on the primary form (the one with the subform), and that works. So I'm left thinking there's some trick to working with the subform.

By the way, if I watch the code in the debugger, the RaiseEvent method appears not to work. There's no error; it just stops on the line with the secondary form open.

From the subform:
Public Event NewMeeting(varMeetingID as Variant)

Within the button's code:
RaiseEven NewMeeeting(Me.MeetingID)

From the secondary form:
Dim varMeetingID As Variant
Dim WithEvents frmMtg As Form_frmsubform

Private Sub Form_Load()
     Set frmMtg = Form_subform
End Sub

Private Sub frmMtg_NewMeeting(varID As Variant)
    varMeetingID = varID
    If IsNothing(varID) Then
        Me.Visible = False
    Else
        Me.Filter = "MeetingID = " & varID
        Me.FilterOn = True
        Me.MeetingID.DefaultValue = "'" & varID & "'"
        Me.Visible = True
    End If
End Sub
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

I'd try this:

Private Sub Form_Load()
     Set frmMtg = Me.NameOfYourSubFormControl.Form
End Sub

Not sure this will hook the event model of your subform, but it should.


Avatar of puppydogbuddy
puppydogbuddy

I believe your problem may just be due to a slight timing difference between when frmMtg loses focus and when the subform gets focus.  You don't want to set your pointer to the subform unless the subform is loaded.

Try changing this:
            Private Sub Form_Load()
                 Set frmMtg = Form_subform
            End Sub

to this:
            Private Sub Form_Load()
            If IsLoaded(Form_subform) Then
                 Set frmMtg = Form_subform
            End If
            End Sub

Avatar of JeffreyWest

ASKER

Nope to both.
try this:
            Private Sub Form_Load()
            If IsLoaded(Form_subform) Then
                 Set frmMtg = Me!YourSubformControl.Form!Form_subform
            End If
            End Sub

 

replace "YourSubformControl" with the actual name of your subform control on the main form.
Hello JeffreyWest

> "Nope to both."

That is not very helpful. You could for example have answered something along those lines:

"Thank you for your quick reply. However, I do not understand your suggestion. My secondary form does not have a subform. Would you please explain your idea?"

Or any other problem you had. I tried your setup and it works without any problem (after removing the few mistakes with make your code above uncompilable, naturally). So where is the problem?

As LSMC {http:#16876834} already said, it is very bad practice to use:

     Set frmMtg = Form_subform   ' btw, cf: frmMtg As Form_frmsubform !

You are assigning a class to an object variable. That is very unsafe and not accepted in other programming languages. VB being traditionally very forgiving, it will attempt to find an open instance of that class, or even create one for you! But you cannot control which instance you get. By using the full reference of the form, you get exactly the instance you want.

There could be several other possible problems. For example if you leave the secondary form open while you develop, or if you reset your VB code for some reason. What happens if you add this event and select your secondary form?

Private Sub Form_Activate()
     frmMtg.Visible = True
End Sub

Have fun!
(°v°)
First I was not trying to be flip or unhelpful. I will take your comments as I hope they were intended. Second, I would categorize myself as a dangeous novice. I know enough to follow and tweak examples. Sometimes it works, and sometimes it doesn't. I had no idea the comments from LSMC should have told me that was bad practice. It's working flawless in the example I'm following; it's just not working for me. I placed the failure on my abilites, not those of the author.

Form the suggestions above, I don't believe that it's a form timing issue. The click action to open the secondary form, (the one that should get the index number via the event), occurs before the raise event.

<Snip>
    DoCmd.OpenForm "frmMemberAttendance_ssn", WhereCondition:="MeetingID = 0" 'opens the secondary form
    DoEvents
    RaiseEvent NewMeeting(Me.MeetingID) 'should raise the event, but nothing catches it.
<End Snip>

So now after much fiddling, the previously working method now functions half the time.

I'm not particularly wedded to using this approach. I want to open a second form and have that form know/remember what the same number was on the other form. The business case is to record who attended a meeting. The meeting index is known (from the primary form), and I need to get that number to the second form so I can add people.
The secondary form is the table between a many-many relationship. So I need the number from the primary form (the meeting number) to populate the seconday form so I can add a person to this many-many in between table. The  meeting is half the primary key of the table. The person is the otehr half.
Try this:

<Snip>
    DoCmd.OpenForm "frmMemberAttendance_ssn", WhereCondition:=nz("MeetingID,0) = 0" 'opens the secondary form
ASKER CERTIFIED SOLUTION
Avatar of Markus Fischer
Markus Fischer
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial