By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Class with custom event as subform wrapper

P: n/a
Hello,

I am trying to figure out how to use a class with a custom event as a
wrapper for a subform so that I can automatically trigger things to
happen on the parent form when the custom event is raised. I am new to
custom events, so please bear with me...

I have created a class module (clsMyEvent) as follows:

Option Compare Database
Option Explicit

Private WithEvents mcmd As CommandButton
Public Event MyEvent()

Public Property Set ActionButton(cmd As CommandButton)

Set mcmd = cmd

End Property

Private Sub mcmd_Click()

RaiseEvent MyEvent

End Sub

I then created a form with a single command button on it (cmdRaise) and
set its HasModule property to False. I then created a second form and
embedded the first form in it as a subform and named the subform
'fsub'. Then I added the following code to the second form:

Option Explicit

Dim WithEvents mMyEvent As clsMyEvent

Private Sub Form_Load()

Set mMyEvent = New clsMyEvent
Set mMyEvent.ActionButton = Me.fsub.Form.Controls("cmdRaise")

End Sub

Private Sub mMyEvent_MyEvent()

MsgBox "My event!"

End Sub

In this simple example, the intent is to be able to click the command
button on the subform thus triggering display of a messagebox from the
parent form by raising the MyEvent event. However, nothing happens
when I click the command button. It all compiles fine and I get no
errors, but I also get no message box. What am I doing wrong? Any
help greatly appreciated...thanks!

Bruce

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
rkc
br***@aristotle.net wrote:
Hello,

I am trying to figure out how to use a class with a custom event as a
wrapper for a subform so that I can automatically trigger things to
happen on the parent form when the custom event is raised. I am new to
custom events, so please bear with me...

I have created a class module (clsMyEvent) as follows:

Option Compare Database
Option Explicit

Private WithEvents mcmd As CommandButton
Public Event MyEvent()

Public Property Set ActionButton(cmd As CommandButton)

Set mcmd = cmd

End Property

Private Sub mcmd_Click()

RaiseEvent MyEvent

End Sub

I then created a form with a single command button on it (cmdRaise) and
set its HasModule property to False. I then created a second form and
embedded the first form in it as a subform and named the subform
'fsub'. Then I added the following code to the second form:


Setting the HasModule property of the subform to false is why the event
isn't firing. Change that and your code should work.
Nov 13 '05 #2

P: n/a
rkc
br***@aristotle.net wrote:
One edition to your class.
You have to set the mcmd.OnClick event to an Event Procedure.
So:
1) Set the HasModule property of your fSub form to True
2) Add mcmd.OnClick = "[Event Procedure]"
to the ActionButton property set procedure.
Public Property Set ActionButton(cmd As CommandButton)

Set mcmd = cmd mcmd.OnClick = "[Event Procedure]"

End Property

Nov 13 '05 #3

P: n/a
Thank you RKC...I knew it must be something simple I was overlooking!

Bruce

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.