Sub form raising event for a master form

michaellysons

Registered User.
Local time
Today, 15:25
Joined
May 7, 2004
Messages
11
Hi

I'd like to make a sub form raise an event that can be trapped by the master form. Is this possible in Access? I have tried doing it and while I can create and raise the event, the master form seems unable to trap that the event has fired.

For example, in the sub form I have some code like;

Public Event RecordChanged
.
.
.
Private Sub MySub()

RaiseEvent RecordChanged

End Sub


Then, in the master form I want to do something like;

Private Sub SubForm_RecordChanged()

Msgbox "Record changed in sub form."

End Sub

but the master form does not appear able to trap this event. Note that the event above is merely an example.

Any help greatly appreciated.

Thanks

Michael
 
Public Sub?

Why not try this:

Instead of defining the routine on the master form as Private, define it as Public:

Public Sub SubForm_RecordChanged().

You'll now be able to call this routine from the subform.

e.g. on the subform's BeforeUpdate event you could insert the code

Code:
    Form_Main_Form.SubForm_RecordChanged

This will call the SubForm_RecordChanged routine which belongs to the form "Main_Form".

An Access form with a module is regarded as a class (I think). For coding purposes, the name of the class associated with a form is the form's name prefixed by "Form_". So if you called your form "Main_Form" and you attached some code to it, the class for this form would be Form_Main_Form.

Functions and subs that have been declared as "Public" are available everywhere in your project.
 
That's a great tip, thanks. I wasn't aware you could call subs/functions in that way. I've tried it out and it works a treat. In fact, it works so well, it's solved another problem I was working on.

Thanks again.
 

Users who are viewing this thread

Back
Top Bottom