Solved

VBA AddHandler????

Posted on 2011-02-21
11
2,028 Views
Last Modified: 2012-08-14
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
Comment
Question by:tonelm54
  • 6
  • 5
11 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947042
What kind of checkbox (Forms or ActiveX), and where are you adding it?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947051
If it's Forms, then you set its OnAction property to the name of the macro you want to run.
0
 

Author Comment

by:tonelm54
ID: 34947150
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947191
Where are you adding it to? A Userform or a worksheet?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947195
(that's not, oddly, the Forms control I was referring to)
0
 

Author Comment

by:tonelm54
ID: 34947215
It's a userform

Thank you
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947236
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
 

Author Comment

by:tonelm54
ID: 34949103
Err, do you have any sample code for that? Sounds a very complex way of doing a simple task :-S
0
 

Author Comment

by:tonelm54
ID: 34950161
My code I currently have is:-
0
 

Author Comment

by:tonelm54
ID: 34950164
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34950223
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting error in connectionstring with Excel. 30 35
locking multiple column ranges 10 25
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
Excel VBA - Constants 4 17
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question