Solved

Raising an Event from a subform

Posted on 2006-06-10
9
813 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:JeffreyWest
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 84
ID: 16876834
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.


0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 16877210
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

0
 

Author Comment

by:JeffreyWest
ID: 16878362
Nope to both.
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 16878716
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 58

Expert Comment

by:harfang
ID: 16879287
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°)
0
 

Author Comment

by:JeffreyWest
ID: 16881267
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.
0
 

Author Comment

by:JeffreyWest
ID: 16881271
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.
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 16881345
Try this:

<Snip>
    DoCmd.OpenForm "frmMemberAttendance_ssn", WhereCondition:=nz("MeetingID,0) = 0" 'opens the secondary form
0
 
LVL 58

Accepted Solution

by:
harfang earned 500 total points
ID: 16882192
Hello JeffreyWest

Rereading my comment, it was indeed a little ironic, sorry for that. Please understand that the techniques you are using are normally not encountered when starting with VB, and aren't even known by several seasoned programmers I know.

I must have assumed you were "seasoned" and were trying out events as an experiment.

As it stands, you want this sequence of events when a user presses a button:
 • Open a form
 • Set its filter to a condition
 • Set the default value for a control.

The first two are acheived by the OpenForm action, the second is a single line of VB. You do not really need an event for that because it's not really an event (something that can happen or not in an unpredictable pattern), but rather just an action.

Anyway, this should do it:

    If IsNull(Me.MeetingID)
        ' nothing to do?
    Else
        DoCmd.OpenForm "frmMemberAttendance_ssn", _
            WhereCondition:="MeetingID=" & Me.MeetingID
        Forms!frmMemberAttendance_ssn!MeetingID.DefaultValue = Me.MeetingID
    End If

As you see, there is no need for an event handler. As a counter example, you could use a RaiseEvent every time a new Meeting is created. Not specifically for one form, but instead for any form that might be "listening". Imagine three or four forms which would react in some way in that event, but your main form does not know which of these are open at any given time.

So a RaiseEvent is normally a call out into the blue.

Another example would use similar code as yours: setting a "WithEvents" object variable to the subform, but not a custom event, simple the form's Current event. The subform would of course have "[Event Procedure]" selected (which means "raise an event in that case"), even if no event procedure exists in that form's module. Instead, you would have a frmMtg_Current() event handler, much like your custom frmMtg_NewMeeting().

This would be a way to mimic the behavior of a linked subform, but as a stanalone form, only if it's open.

I don't know if my couter examples are clear, but in any case, you do not need custom events in this setup. If you do want to make it work nontheless, use this to grab the subform's class:

    Set frmMtg <mail form name>!<subform control>.Form

Cheers!
(°v°)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now