Connecting Tech Pros Worldwide Help | Site Map

Class with custom event as subform wrapper

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 13th, 2005, 02:56 PM
bruce@aristotle.net
Guest
 
Posts: n/a
Default Class with custom event as subform wrapper

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


  #2  
Old November 13th, 2005, 02:57 PM
rkc
Guest
 
Posts: n/a
Default Re: Class with custom event as subform wrapper

bruce@aristotle.net wrote:[color=blue]
> 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:[/color]

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


  #3  
Old November 13th, 2005, 02:57 PM
rkc
Guest
 
Posts: n/a
Default Re: Class with custom event as subform wrapper

bruce@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.
[color=blue]
> Public Property Set ActionButton(cmd As CommandButton)
>
> Set mcmd = cmd[/color]
mcmd.OnClick = "[Event Procedure]"
[color=blue]
>
> End Property[/color]
  #4  
Old November 13th, 2005, 02:57 PM
bruce@aristotle.net
Guest
 
Posts: n/a
Default Re: Class with custom event as subform wrapper

Thank you RKC...I knew it must be something simple I was overlooking!

Bruce

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.