VBA

Moderators: PavlinII
Number of threads: 1614
Number of posts: 3000

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Creating an Event handler Posted by GideonOmega on 6 Dec 2005 at 1:31 PM
Hello everyone, I'm not sure if this is possible in VBA but here goes.

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.

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:

Private Sub CheckBox1_Click()
 ...code here
 ...code here
End Sub
Private sub Checkbox2_Click()
 ...code here
 ...code here
End sub
etc...
etc...
etc...


I could have something like
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


has anyone ever done anything like this before? any thoughts.

C:\Dos
C:\Dos Run
Run Dos Run

Report
Re: Creating an Event handler - bad news Posted by PavlinII on 16 Dec 2005 at 1:36 PM
: Hello everyone, I'm not sure if this is possible in VBA but here goes.
:
: 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.
:
: 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:
:
:
: Private Sub CheckBox1_Click()
:  ...code here
:  ...code here
: End Sub
: Private sub Checkbox2_Click()
:  ...code here
:  ...code here
: End sub
: etc...
: etc...
: etc...
: 

:
: I could have something like
:
: 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
: 

:
: has anyone ever done anything like this before? any thoughts.
:
: C:\Dos
: C:\Dos Run
: Run Dos Run
:

:

Hi,
I've got bad news for you..
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..
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.. :(

You'll have to write a lot of
Private Sub CheckBox1_Click()
 ...code here
 ...code here
End Sub
Private sub Checkbox2_Click()
 ...code here
 ...code here
End sub
etc...
etc...
etc...

blocks.. I suggest you to give just one line of code to each of them..
Private sub Checkbox2_Click()
  HandleClick(2)
End sub
...
Private Sub HandleClick(Byval Index as Integer)
  If Index=2 then...


If anyone does know better solution, please, correct me..


Pavlin II[/size]

Don't take life too seriously anyway you won't escape alive from it!


Report
Re: Creating an Event handler - bad news Posted by GideonOmega on 17 Dec 2005 at 4:41 PM
: : Hello everyone, I'm not sure if this is possible in VBA but here goes.
: :
: : 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.
: :
: : 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:
: :
: :
: : Private Sub CheckBox1_Click()
: :  ...code here
: :  ...code here
: : End Sub
: : Private sub Checkbox2_Click()
: :  ...code here
: :  ...code here
: : End sub
: : etc...
: : etc...
: : etc...
: : 

: :
: : I could have something like
: :
: : 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
: : 

: :
: : has anyone ever done anything like this before? any thoughts.
: :
: : C:\Dos
: : C:\Dos Run
: : Run Dos Run
: :

: :
:
: Hi,
: I've got bad news for you..
: 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..
: 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.. :(
:
: You'll have to write a lot of
:
: Private Sub CheckBox1_Click()
:  ...code here
:  ...code here
: End Sub
: Private sub Checkbox2_Click()
:  ...code here
:  ...code here
: End sub
: etc...
: etc...
: etc...
: 

: blocks.. I suggest you to give just one line of code to each of them..
:
: Private sub Checkbox2_Click()
:   HandleClick(2)
: End sub
: ...
: Private Sub HandleClick(Byval Index as Integer)
:   If Index=2 then...
: 

:
: If anyone does know better solution, please, correct me..
:
:
: Pavlin II[/size]
:
: Don't take life too seriously anyway you won't escape alive from it!
:
:
:



Thanks for the Reply - sorry I haven't been back here sooner I did find a simple solution to my problem.

first you add a class module to you vba project and create a simple var
dim mycheck as checkbox
private sub mycheck_click()
 'add all relevent code here
end sub


then you create a global array of you new class in your document.

then in your init event for your document
you iterate through the checkboxs in your document and redim your new array then set the ubound to equal the current checkbox.

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.

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.

it works great.





C:\Dos
C:\Dos Run
Run Dos Run


Report
Re: Creating an Event handler - bad news Posted by DougieW on 6 Jun 2009 at 7:01 AM
Just found your post from 2005. I have the exact same problem and would appreciate a copy the code you found

DougieW
Report
Re: Creating an Event handler - bad news Posted by izthar on 4 Mar 2012 at 12:14 PM
I found a solution!!!

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.

class Module "clsLabel" code:

Public WithEvents myLabel As MSForms.Label

Private Sub myLabel_Click()
msgbox("myLabel Event")
End Sub

user form code:
Private WithEvents lbl As Label
Private newLabel() As clsLabel

Private Sub UserForm_Initialize()
Dim i As Integer
Dim etiq As String

For i = 1 To 42
etiq = "lbl" & i
Set lbl = frmCalendario.Controls.Item(etiq)
Set newLabel(i) = New clsLabel
Set newLabel(i).myLabel = lbl
Next i
End Sub

And ... it works!!!!!
Report
Re: Creating an Event handler - bad news Posted by izthar on 4 Mar 2012 at 12:16 PM
I found a solution!!!

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.

class Module "clsLabel" code:

Public WithEvents myLabel As MSForms.Label

Private Sub myLabel_Click()
msgbox("myLabel Event")
End Sub

user form code:
Private WithEvents lbl As Label
Private newLabel() As clsLabel

Private Sub UserForm_Initialize()
Dim i As Integer
Dim etiq As String

For i = 1 To 42
etiq = "lbl" & i
Set lbl = frmCalendario.Controls.Item(etiq)
Set newLabel(i) = New clsLabel
Set newLabel(i).myLabel = lbl
Next i
End Sub

And ... it works!!!!!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.