Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating an Event handler

GideonOmegaGideonOmega Posts: 617Member
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:

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

I could have something like
[code]
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
[/code]

has anyone ever done anything like this before? any thoughts.
[blue]
C:Dos
C:Dos Run
Run Dos Run
[/blue]

Comments

  • PavlinIIPavlinII Posts: 404Member
    : 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:
    :
    : [code]
    : Private Sub CheckBox1_Click()
    : ...code here
    : ...code here
    : End Sub
    : Private sub Checkbox2_Click()
    : ...code here
    : ...code here
    : End sub
    : etc...
    : etc...
    : etc...
    : [/code]
    :
    : I could have something like
    : [code]
    : 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
    : [/code]
    :
    : has anyone ever done anything like this before? any thoughts.
    : [blue]
    : C:Dos
    : C:Dos Run
    : Run Dos Run
    : [/blue]
    :

    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
    [code]
    Private Sub CheckBox1_Click()
    ...code here
    ...code here
    End Sub
    Private sub Checkbox2_Click()
    ...code here
    ...code here
    End sub
    etc...
    etc...
    etc...
    [/code]
    blocks.. I suggest you to give just one line of code to each of them..
    [code]
    Private sub Checkbox2_Click()
    HandleClick(2)
    End sub
    ...
    Private Sub HandleClick(Byval Index as Integer)
    If Index=2 then...
    [/code]

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


    [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

    [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]


  • GideonOmegaGideonOmega Posts: 617Member
    : : 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:
    : :
    : : [code]
    : : Private Sub CheckBox1_Click()
    : : ...code here
    : : ...code here
    : : End Sub
    : : Private sub Checkbox2_Click()
    : : ...code here
    : : ...code here
    : : End sub
    : : etc...
    : : etc...
    : : etc...
    : : [/code]
    : :
    : : I could have something like
    : : [code]
    : : 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
    : : [/code]
    : :
    : : has anyone ever done anything like this before? any thoughts.
    : : [blue]
    : : C:Dos
    : : C:Dos Run
    : : Run Dos Run
    : : [/blue]
    : :
    :
    : 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
    : [code]
    : Private Sub CheckBox1_Click()
    : ...code here
    : ...code here
    : End Sub
    : Private sub Checkbox2_Click()
    : ...code here
    : ...code here
    : End sub
    : etc...
    : etc...
    : etc...
    : [/code]
    : blocks.. I suggest you to give just one line of code to each of them..
    : [code]
    : Private sub Checkbox2_Click()
    : HandleClick(2)
    : End sub
    : ...
    : Private Sub HandleClick(Byval Index as Integer)
    : If Index=2 then...
    : [/code]
    :
    : If anyone does know better solution, please, correct me..
    :
    :
    : [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
    :
    : [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]
    :
    :
    :



    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
    [code]
    dim mycheck as checkbox
    private sub mycheck_click()
    'add all relevent code here
    end sub
    [/code]

    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.




    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • DougieWDougieW Posts: 1Member
    Just found your post from 2005. I have the exact same problem and would appreciate a copy the code you found

    DougieW
  • iztharizthar Posts: 2Member
    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!!!!!
  • iztharizthar Posts: 2Member
    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!!!!!
Sign In or Register to comment.