Connecting Tech Pros Worldwide Help | Site Map

Class with custom event as subform wrapper

  #1  
Old November 13th, 2005, 03:56 PM
bruce@aristotle.net
Guest
 
Posts: 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

  #2  
Old November 13th, 2005, 03:57 PM
rkc
Guest
 
Posts: n/a

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, 03:57 PM
rkc
Guest
 
Posts: n/a

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, 03:57 PM
bruce@aristotle.net
Guest
 
Posts: n/a

re: Class with custom event as subform wrapper


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

Bruce

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disadvantage of Access as front-end Yannick Turgeon answers 49 November 13th, 2005 02:37 AM