8 Replies Latest reply on Dec 2, 2015 12:44 PM by JOHN GEORGE

    How to add an event if change occured in combo box or text box

    JOHN GEORGE

      I am trying to add an event like a button show or hide or add a label if any of these current fields change in the userform

      These are either combo box or text box

      Some of these values are already there from the part properties

      I looked thru the VB help files and couldn't find a working solution for this.

      Can someone please help me with a proper code.

      Thanks!

      userform-properties.png

        • Re: How to add an event if change occured in combo box or text box
          Digvijay Taunk

          The simplest way is just double click on the control in which you want to add code for its event changed.

          For example, double click on text box : - it automatically adds text change event in the code editor. You can add more events from code editor, just select the control from list of control & event from event list. see screenshot of macro code editor for example.

           

          events.jpg

           

          For Hide/Show property use Control.Visible Property

          e.g. CommandButton1.Visible = True

            • Re: How to add an event if change occured in combo box or text box
              JOHN GEORGE

              Digvijay,

              Thanks for the reply.

              Your solution works for individual controls

              Is it possible to combine all these together.

              For example, if any of the combo box or text box changes its value, need to trigger an event like CommandButton1.Visible = True

                • Re: How to add an event if change occured in combo box or text box
                  Karim Said

                  I think you can do that by 2 ways:

                  1- You can use a shared event, if both initial events have the same structure, you can do that by first double click the text box to add a text changed event, then get the event list for the combo box and on the value changed event, do not double click, use the drop down menu to choose the event handler, in this case reuse the event created for the text box.

                  2- If this isn't possible, create a function that toggles the visibility state, double click each control and call the function in both events.

                   

                  I hope that helps.

                  • Re: How to add an event if change occured in combo box or text box
                    Digvijay Taunk

                    John,

                    Events are the signals that informs application that something has happened, for e.g text entered in textbox or, selection changed in combobox.

                    If you want to change the visibility property of all controls, you have to do it line by line only. there is no way to combine property of controls.

                     

                    From your e.g I understand that you want to change same property from two different events, for that you need to write same line at both event block (In combobox selection changed as well as textbox text changed). If any of theses events triggered it will change the control's visibility property. Other way is to create a sub routine which will include all the controls visibility property, and call it

                     

                    Sample code for macro will be like :

                    Private Sub ComboBox1_Change()
                     If ComboBox1.ListIndex = 0 Then
                            CommandButton1.Visible = True
                            Label1.Visible = True
                        Else
                            CommandButton1.Visible = False
                            Label1.Visible = False
                        End If
                    End Sub
                    
                    
                    Private Sub TextBox1_Change()
                        If TextBox1.Text = "s" Then
                            CommandButton1.Visible = True
                            Label1.Visible = True
                        Else
                            CommandButton1.Visible = False
                            Label1.Visible = False
                        End If
                    End Sub
                    
                    Private Sub UserForm_Initialize()
                        ComboBox1.AddItem ("Show")
                        ComboBox1.AddItem ("Hide")
                    End Sub
                    

                     

                • Re: How to add an event if change occured in combo box or text box
                  幸 柴 田

                  I think you can use "WithEvents" .

                   

                  First you need to Add the class module.

                  And name it "MultiEvent"

                   

                  "MultiEvent" class code is this

                  -------------------------------------------------------

                  Public WithEvents cTextBox As TextBox

                   

                  Private Sub cTextBox_Change()

                        MsgBox cTextBox.Name & " has changed"

                  End Sub

                  -------------------------------------------------------

                   

                  "UserForm1" code is this

                  -------------------------------------------------------

                  'You can add more TextBoxs here

                  Dim Multi(1) As New MultiEvent

                   

                  Private Sub UserForm_Initialize()

                    Set Multi(0).cTextBox = UserForm1.TextBox1

                    Set Multi(1).cTextBox = UserForm1.TextBox2

                  End Sub

                  -------------------------------------------------------

                   

                  Run this code You will see msgbox  when you change TextBox1 or 2

                    • Re: How to add an event if change occured in combo box or text box
                      JOHN GEORGE

                      Kou,

                       

                      This gives the message as soon as the user form open

                      My sequence is as follows

                      Run this macro to open the user form

                      Either enter text box values or modify the existing one

                      Select the values from different drop down boxes

                      Click "Apply"  (still the form is open)

                      Now if I change any of these fields, I need an action on this event change

                       

                      Hope this makes clear

                      Thanks!

                        • Re: How to add an event if change occured in combo box or text box
                          幸 柴 田

                          How about this way?

                          This time, message will show after click command button1.

                           

                           

                          main code

                          class module code

                          --------------------------------------------------

                          Public WithEvents cTextBox As TextBox

                          Public blCheck As Boolean

                           

                           

                          Public Sub Bind(cTextBox As TextBox, blCheck As Boolean)

                            Set Me.cTextBox = cTextBox

                            Me.blCheck = blCheck

                          End Sub

                           

                           

                          Private Sub cTextBox_Change()

                            If blCheck = True Then

                              MsgBox cTextBox.Name & " has changed after click the button"

                            Else

                           

                           

                            End If

                           

                          End Sub

                          UserForm1 code

                          ------------------------------------------------

                          Dim Multi(1) As New MultiEvent

                          Dim blCheckForm As Boolean

                           

                          Private Sub UserForm_Initialize()

                           

                          blCheckForm = False

                              Multi(0).Bind UserForm1.TextBox1, blCheckForm

                              Multi(1).Bind UserForm1.TextBox2, blCheckForm

                          End Sub

                           

                          Private Sub CommandButton1_Click()

                             blCheckForm = True

                             Multi(0).Bind UserForm1.TextBox1, blCheckForm

                             Multi(1).Bind UserForm1.TextBox2, blCheckForm

                          End Sub

                          UserForm Design