Solved

How do I group Excel user form buttons with the Click event ?

Posted on 2014-01-06
9
369 Views
Last Modified: 2014-01-06
Hi,

I have an Excel VBA user form with a multi-page control (of 6 pages) each containing a single command button.

I want to be able to detect which of the (6) buttons has been clicked without resorting to coding separate procedures for each of the buttons by name
eg. Sub cbCalculate1_Click, Sub cbCalculate2_Click, Sub cbCalculate3_Click etc.......

I had a similar requirement with text boxes on the user form and an EE expert provided me with the code to resolve this issue. He set up a Class module and checked the text box name to establish which of the text boxes had been changed. The functionality works fine.

So, I've tried to adapt his code and apply it to my button problem. But when I click on a button nothing happens. I've tried it in debug and the Class module procedure isn't even invoked. I can only assume I don't have the correct syntax to 'catch' this click event.

Here's the code I have so far.
In a Class module I have called clsCalculate...

Option Explicit

Public WithEvents aButton  As MSForms.CommandButton

Private Sub aButton_Click()

   Dim Idx As Integer

   If aButton.Name Like "cbCalculate*" Then
      Idx = CInt(Mid(aButton.Name, 12, 1)) ' Which group?
      Call ufJobsForm.Calculate_Prices(Idx)
   End If

End Sub

Open in new window


The above Class procedure should pass an argument (Idx) to the form module procedure Calculate_Prices but when I click on a button it doesn't do anything.

Your help will be much appreciated.

Toco
0
Comment
Question by:Tocogroup
  • 5
  • 4
9 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39759010
Hi,

How do you initialze your command buttons?

Regards
0
 

Author Comment

by:Tocogroup
ID: 39759031
Hi,

Ah, I don't ! Didn't know I'd need to.

Prior to attempting to adopt your text box code for the buttons, I called each button by name with 6 separate procedures.
0
 

Author Comment

by:Tocogroup
ID: 39759051
How should I initialise the buttons in my example ?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39759134
Hi,

pls try

in the form

Dim myCmdBtns() As clsCalculate

Private Sub UserForm_Initialize()
    Dim ControlItem As Object, pointer As Long
    ReDim myCmdBtns(1 To Me.Controls.Count)
    For Each ControlItem In Me.Controls
        If TypeName(ControlItem) = "CommandButton" Then
            pointer = pointer + 1
            Set myCmdBtns(pointer) = New clsCalculate
            Set myCmdBtns(pointer).aButton = ControlItem
        End If
    Next ControlItem
    ReDim Preserve myCmdBtns(1 To pointer)
End Sub

Open in new window

Regards
0
 

Author Comment

by:Tocogroup
ID: 39759824
Ah, yes. I see what you mean. It's the same as you suggested for the text box code. I've got it to work thanks.

Can I use the same approach for a double-click event on a textbox field ? Like so.....

Public WithEvents aConfirmation  As MSForms.TextBox

Private Sub aConfirmation_DblClick()

   Dim Idx As Integer

   If aConfirmation.Name Like "teConfirmation*" Then
      Idx = CInt(Mid(aConfirmation.Name, 15, 1)) ' Which group?
      Call ufJobsForm.Create_Confirmation(Idx)
   End If

End Sub

Do I need to change the textbox initialize code for it to accept the double-click event ?
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39759854
Yes that's right

you have to initialize each control as a corresponding Class to be able to manipulate them as Class

Regards
0
 

Author Comment

by:Tocogroup
ID: 39759919
The problem is it's giving me a compilation error as I'm using the same initialization code for both types of textbox control:
a) textbox controls which get changed
b) other textbox controls on which I double-click.

How do I differentiate between the two types (or events) of textbox control ? This is the initialisation code you gave me for a change of value...

   For Each ControlItem In Me.Controls
      If TypeName(ControlItem) = "TextBox" Then
         Pointer = Pointer + 1
         Set myTextBx(Pointer) = New clsTextBx
         Set myTextBx(Pointer).aTextBox = ControlItem
      End If
   Next ControlItem

.aTextBox is used by the Class procedure when a textbox changes, whereas, I also need to set a separate Class control (.aConfirmation) for a different textbox when I double-click on that.
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39759952
If your remember

in your txtbox Class

you have the Change Event

Private Sub aTextBox_Change()
If aTextBox.Name Like "teDelegateName*" Then
   Call UserForm1.TextBxCountDelegates
End If

Open in new window


You could add a double click Event for the other Textboxes
Private Sub aTextBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Not(aTextBox.Name Like "teDelegateName*") Then
'   Call Another code
End If

Open in new window

Regards
0
 

Author Closing Comment

by:Tocogroup
ID: 39760024
Ah ! I hadn't included the (ByVal Cancel As MSForms.ReturnBoolean) as the argument. Not sure what that does though.

Anyway, it works perfectly.

Thanks again for your expert assistance.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

820 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