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

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
TocogroupAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Rgonzo1971Connect With a Mentor Commented:
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
 
Rgonzo1971Commented:
Hi,

How do you initialze your command buttons?

Regards
0
 
TocogroupAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
TocogroupAuthor Commented:
How should I initialise the buttons in my example ?
0
 
Rgonzo1971Commented:
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
 
TocogroupAuthor Commented:
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
 
Rgonzo1971Commented:
Yes that's right

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

Regards
0
 
TocogroupAuthor Commented:
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
 
TocogroupAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.