Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2264
  • Last Modified:

VBA AddHandler????

If I add a checkbox via code at run-time, how can add code to fire when the user clicks on it?

In VB.net I would use addhandler, but I know this isnt what I need to do with VBA.

Reading up, I thought I could just use chkNew.onClick myMethod(), but that doesnt work either.

Does anyone have an exampe of how to add a handler to a CheckBox in VBA?

Thank you
0
tonelm54
Asked:
tonelm54
  • 6
  • 5
1 Solution
 
Rory ArchibaldCommented:
What kind of checkbox (Forms or ActiveX), and where are you adding it?
0
 
Rory ArchibaldCommented:
If it's Forms, then you set its OnAction property to the name of the macro you want to run.
0
 
tonelm54Author Commented:
I'm using MSForms.CheckBox, but looking at the intellisence there is no inaction property I can assign my macro to. I need to be able to add it by code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rory ArchibaldCommented:
Where are you adding it to? A Userform or a worksheet?
0
 
Rory ArchibaldCommented:
(that's not, oddly, the Forms control I was referring to)
0
 
tonelm54Author Commented:
It's a userform

Thank you
0
 
Rory ArchibaldCommented:
In that case, your best bet is to use a class with a Checkbox variable declared WithEvents and put your Click code in that. Then as you add each checkbox, create an instance of the class, assign the checkbox to the class's variable and then add the class instance to a Collection (declared at module level in the userform).
Do you need sample code for that?
0
 
tonelm54Author Commented:
Err, do you have any sample code for that? Sounds a very complex way of doing a simple task :-S
0
 
tonelm54Author Commented:
My code I currently have is:-
0
 
tonelm54Author Commented:
ok, this time I attach the code!!!

Private Sub UserForm_Activate()
      Dim newCheckBox  As MSForms.CheckBox
        
        Set newCheckBox = Frame1.Controls.Add("Forms.CheckBox.1")
        newCheckBox.Caption = "Hello " & x
        newCheckBox.Top = x * 30
        newCheckBox.Left = 10
        
        newCheckBox.OnAction = "Checkbox_Toggle"
                
        Frame1.Controls.Add newCheckBox
End Sub

Open in new window


Sorry!
0
 
Rory ArchibaldCommented:
Class module called CheckboxHandler:
Option Explicit

Public WithEvents cb As MSForms.CheckBox

Private Sub cb_Click()
   MsgBox "You clicked checkbox: " & cb.Name
End Sub

Open in new window


Userform code:
Option Explicit

Dim colCBHandlers As Collection

Private Sub UserForm_Activate()
   Dim objHandler As CheckboxHandler
 Dim x As Integer
   Set colCBHandlers = New Collection
   
    For x = 1 To 100 Step 1
        Dim newCheckBox  As MSForms.CheckBox
        Set newCheckBox = Me.Frame1.Controls.Add("Forms.CheckBox.1")
        newCheckBox.Caption = "Hello " & x
        newCheckBox.Top = x * 30
        newCheckBox.Left = 10
        ' create new handler
        Set objHandler = New CheckboxHandler
        ' assign checkbox
        Set objHandler.cb = newCheckBox
        colCBHandlers.Add objHandler
    Next x

End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now