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

x
?
Solved

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

Posted on 2014-01-06
9
Medium Priority
?
396 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 53

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

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 53

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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

971 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