<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Creating an Event handler' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Creating an Event handler' posted on the 'VBA' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 25 May 2013 13:17:41 -0700</pubDate>
    <lastBuildDate>Sat, 25 May 2013 13:17:41 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Creating an Event handler</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/323244/creating-an-event-handler/</link>
      <description>Hello everyone, I'm not sure if this is possible in VBA but here goes.&lt;br /&gt;
&lt;br /&gt;
I am trying to create an event handler in vba - MS Word - to handle all of the click events for a series of check box's that I have in a table.&lt;br /&gt;
&lt;br /&gt;
The reason that I want to set up a single event handler is there is a large number of check box's so instead of having:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
Private Sub CheckBox1_Click()
 ...code here
 ...code here
End Sub
Private sub Checkbox2_Click()
 ...code here
 ...code here
End sub
etc...
etc...
etc...
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
I could have something like&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
Private sub ActiveDocument.shapes_Click( obj at ActiveDocument.Shapes.OLEFormat)
     if obj.progID = "Forms.CheckBox.1" then
        ...code for event here
     end if
end Sub
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
has anyone ever done anything like this before? any thoughts.&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;br /&gt;
C:\Dos&lt;br /&gt;
C:\Dos Run&lt;br /&gt;
Run Dos Run&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/323244/creating-an-event-handler/</guid>
      <pubDate>Tue, 06 Dec 2005 13:31:32 -0700</pubDate>
      <category>VBA</category>
    </item>
    <item>
      <title>Re: Creating an Event handler - bad news</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/324356/re-creating-an-event-handler---bad-news/#324356</link>
      <description>: Hello everyone, I'm not sure if this is possible in VBA but here goes.&lt;br /&gt;
: &lt;br /&gt;
: I am trying to create an event handler in vba - MS Word - to handle all of the click events for a series of check box's that I have in a table.&lt;br /&gt;
: &lt;br /&gt;
: The reason that I want to set up a single event handler is there is a large number of check box's so instead of having:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: Private Sub CheckBox1_Click()
:  ...code here
:  ...code here
: End Sub
: Private sub Checkbox2_Click()
:  ...code here
:  ...code here
: End sub
: etc...
: etc...
: etc...
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: I could have something like&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: Private sub ActiveDocument.shapes_Click( obj at ActiveDocument.Shapes.OLEFormat)
:      if obj.progID = "Forms.CheckBox.1" then
:         ...code for event here
:      end if
: end Sub
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: has anyone ever done anything like this before? any thoughts.&lt;br /&gt;
: &lt;span style="color: Blue;"&gt;&lt;br /&gt;
: C:\Dos&lt;br /&gt;
: C:\Dos Run&lt;br /&gt;
: Run Dos Run&lt;br /&gt;
: &lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
Hi,&lt;br /&gt;
  I've got bad news for you..&lt;br /&gt;
VBA (and VB6) doesn't support multievent handling.. You can not handle multiple events by one procedure and you can not use multiple handlers on one event.. This is possible in VB.NET with AddHandler and RemoveHandler..&lt;br /&gt;
VB6 could help you in this case, when you make your checkboxes dynamically and give them the same name and different indexes.. In this case, you can have one handling sub, BUT VBA in excel doesn't support indexes for controls, as VB6 does.. :(&lt;br /&gt;
&lt;br /&gt;
You'll have to write a lot of &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
Private Sub CheckBox1_Click()
 ...code here
 ...code here
End Sub
Private sub Checkbox2_Click()
 ...code here
 ...code here
End sub
etc...
etc...
etc...
&lt;/pre&gt;&lt;br /&gt;
blocks.. I suggest you to give just one line of code to each of them..&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
Private sub Checkbox2_Click()
  HandleClick(2)
End sub
...
Private Sub HandleClick(Byval Index as Integer)
  If Index=2 then...
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
If anyone does know better solution, please, correct me..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="font-size: medium;"&gt;P&lt;/span&gt;avlin &lt;span style="font-size: medium;"&gt;II&lt;/span&gt;&lt;/em&gt;[/size]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: Purple;"&gt;Don't take life too seriously anyway you won't escape alive from it!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/324356/re-creating-an-event-handler---bad-news/#324356</guid>
      <pubDate>Fri, 16 Dec 2005 13:36:04 -0700</pubDate>
      <category>VBA</category>
    </item>
    <item>
      <title>Re: Creating an Event handler - bad news</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/324454/re-creating-an-event-handler---bad-news/#324454</link>
      <description>: : Hello everyone, I'm not sure if this is possible in VBA but here goes.&lt;br /&gt;
: : &lt;br /&gt;
: : I am trying to create an event handler in vba - MS Word - to handle all of the click events for a series of check box's that I have in a table.&lt;br /&gt;
: : &lt;br /&gt;
: : The reason that I want to set up a single event handler is there is a large number of check box's so instead of having:&lt;br /&gt;
: : &lt;br /&gt;
: : &lt;pre class="sourcecode"&gt;
: : Private Sub CheckBox1_Click()
: :  ...code here
: :  ...code here
: : End Sub
: : Private sub Checkbox2_Click()
: :  ...code here
: :  ...code here
: : End sub
: : etc...
: : etc...
: : etc...
: : &lt;/pre&gt;&lt;br /&gt;
: : &lt;br /&gt;
: : I could have something like&lt;br /&gt;
: : &lt;pre class="sourcecode"&gt;
: : Private sub ActiveDocument.shapes_Click( obj at ActiveDocument.Shapes.OLEFormat)
: :      if obj.progID = "Forms.CheckBox.1" then
: :         ...code for event here
: :      end if
: : end Sub
: : &lt;/pre&gt;&lt;br /&gt;
: : &lt;br /&gt;
: : has anyone ever done anything like this before? any thoughts.&lt;br /&gt;
: : &lt;span style="color: Blue;"&gt;&lt;br /&gt;
: : C:\Dos&lt;br /&gt;
: : C:\Dos Run&lt;br /&gt;
: : Run Dos Run&lt;br /&gt;
: : &lt;/span&gt;&lt;br /&gt;
: : &lt;br /&gt;
: &lt;br /&gt;
: Hi,&lt;br /&gt;
:   I've got bad news for you..&lt;br /&gt;
: VBA (and VB6) doesn't support multievent handling.. You can not handle multiple events by one procedure and you can not use multiple handlers on one event.. This is possible in VB.NET with AddHandler and RemoveHandler..&lt;br /&gt;
: VB6 could help you in this case, when you make your checkboxes dynamically and give them the same name and different indexes.. In this case, you can have one handling sub, BUT VBA in excel doesn't support indexes for controls, as VB6 does.. :(&lt;br /&gt;
: &lt;br /&gt;
: You'll have to write a lot of &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: Private Sub CheckBox1_Click()
:  ...code here
:  ...code here
: End Sub
: Private sub Checkbox2_Click()
:  ...code here
:  ...code here
: End sub
: etc...
: etc...
: etc...
: &lt;/pre&gt;&lt;br /&gt;
: blocks.. I suggest you to give just one line of code to each of them..&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: Private sub Checkbox2_Click()
:   HandleClick(2)
: End sub
: ...
: Private Sub HandleClick(Byval Index as Integer)
:   If Index=2 then...
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: If anyone does know better solution, please, correct me..&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: &lt;span style="color: Blue;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="font-size: medium;"&gt;P&lt;/span&gt;avlin &lt;span style="font-size: medium;"&gt;II&lt;/span&gt;&lt;/em&gt;[/size]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
: &lt;span style="color: Purple;"&gt;Don't take life too seriously anyway you won't escape alive from it!&lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thanks for the Reply - sorry I haven't been back here sooner I did find a simple solution to my problem.&lt;br /&gt;
&lt;br /&gt;
first you add a class module to you vba project and create a simple var&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
dim mycheck as checkbox
private sub mycheck_click()
 'add all relevent code here
end sub
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
then you create a global array of you new class in your document.&lt;br /&gt;
&lt;br /&gt;
then in your init event for your document&lt;br /&gt;
you iterate through the checkboxs in your document and redim your new array then set the ubound to equal the current checkbox.&lt;br /&gt;
&lt;br /&gt;
when this is done - all of the checkbox's respond to the event coded in the class module with out the effort of defining actions for each event.&lt;br /&gt;
&lt;br /&gt;
I don't have th vba code with me - but next week I will grab it and post the actual code so everyone can see.&lt;br /&gt;
&lt;br /&gt;
it works great.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: Blue;"&gt;&lt;br /&gt;
C:\Dos&lt;br /&gt;
C:\Dos Run&lt;br /&gt;
Run Dos Run&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/324454/re-creating-an-event-handler---bad-news/#324454</guid>
      <pubDate>Sat, 17 Dec 2005 16:41:31 -0700</pubDate>
      <category>VBA</category>
    </item>
    <item>
      <title>Re: Creating an Event handler - bad news</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/391998/re-creating-an-event-handler---bad-news/#391998</link>
      <description>Just found your post from 2005. I have the exact same problem and would appreciate a copy the code you found&lt;br /&gt;
&lt;br /&gt;
DougieW&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/391998/re-creating-an-event-handler---bad-news/#391998</guid>
      <pubDate>Sat, 06 Jun 2009 07:01:52 -0700</pubDate>
      <category>VBA</category>
    </item>
    <item>
      <title>Re: Creating an Event handler - bad news</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/427729/re-creating-an-event-handler---bad-news/#427729</link>
      <description>I found a solution!!!&lt;br /&gt;
&lt;br /&gt;
I have a user form named frmCalendario with a frame, the frame contains 42 labels, where de label1 name is lbl1, label2 name is lbl2, ... label42 name is lbl42.&lt;br /&gt;
&lt;br /&gt;
class Module "clsLabel" code:&lt;br /&gt;
&lt;br /&gt;
Public WithEvents myLabel As MSForms.Label&lt;br /&gt;
&lt;br /&gt;
Private Sub myLabel_Click()&lt;br /&gt;
   msgbox("myLabel Event")&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
user form code:&lt;br /&gt;
Private WithEvents lbl As Label&lt;br /&gt;
Private newLabel() As clsLabel&lt;br /&gt;
&lt;br /&gt;
Private Sub UserForm_Initialize()&lt;br /&gt;
     Dim i As Integer&lt;br /&gt;
     Dim etiq As String&lt;br /&gt;
&lt;br /&gt;
     For i = 1 To 42&lt;br /&gt;
        etiq = "lbl" &amp;amp; i&lt;br /&gt;
        Set lbl = frmCalendario.Controls.Item(etiq)&lt;br /&gt;
        Set newLabel(i) = New clsLabel&lt;br /&gt;
        Set newLabel(i).myLabel = lbl&lt;br /&gt;
    Next i&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
And ... it works!!!!!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/427729/re-creating-an-event-handler---bad-news/#427729</guid>
      <pubDate>Sun, 04 Mar 2012 12:14:31 -0700</pubDate>
      <category>VBA</category>
    </item>
    <item>
      <title>Re: Creating an Event handler - bad news</title>
      <link>http://www.programmersheaven.com/mb/vba/323244/427730/re-creating-an-event-handler---bad-news/#427730</link>
      <description>I found a solution!!!&lt;br /&gt;
&lt;br /&gt;
I have a user form named frmCalendario with a frame, the frame contains 42 labels, where de label1 name is lbl1, label2 name is lbl2, ... label42 name is lbl42.&lt;br /&gt;
&lt;br /&gt;
class Module "clsLabel" code:&lt;br /&gt;
&lt;br /&gt;
Public WithEvents myLabel As MSForms.Label&lt;br /&gt;
&lt;br /&gt;
Private Sub myLabel_Click()&lt;br /&gt;
   msgbox("myLabel Event")&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
user form code:&lt;br /&gt;
Private WithEvents lbl As Label&lt;br /&gt;
Private newLabel() As clsLabel&lt;br /&gt;
&lt;br /&gt;
Private Sub UserForm_Initialize()&lt;br /&gt;
     Dim i As Integer&lt;br /&gt;
     Dim etiq As String&lt;br /&gt;
&lt;br /&gt;
     For i = 1 To 42&lt;br /&gt;
        etiq = "lbl" &amp;amp; i&lt;br /&gt;
        Set lbl = frmCalendario.Controls.Item(etiq)&lt;br /&gt;
        Set newLabel(i) = New clsLabel&lt;br /&gt;
        Set newLabel(i).myLabel = lbl&lt;br /&gt;
    Next i&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
And ... it works!!!!!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/vba/323244/427730/re-creating-an-event-handler---bad-news/#427730</guid>
      <pubDate>Sun, 04 Mar 2012 12:16:22 -0700</pubDate>
      <category>VBA</category>
    </item>
  </channel>
</rss>