Selection from combobox and populate multiple fields by separate clicks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ZKAHADI
    New Member
    • Aug 2020
    • 58

    Selection from combobox and populate multiple fields by separate clicks

    Hello friends need your help once again.


    i am trying to make a combobox of education subjects i will put all subjects to one combobox and i made 4 text boxes subject1 subject2 subject3 and subject4.

    now i want to select one subject for example english

    then it must enter to Subject1 another selection Maths to subject2 same others. in simple words one combobox multiple selections to one by one textbox.


    how it will possible ?
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    This sample code looks for a control in UserForm1 that has "subject" in its name. If that value is blank, enter the value you selected in combobox1.

    Code:
    Private Sub ComboBox1_Change()
        For Each cntrl In UserForm1.Controls
            If cntrl.Value = "" And InStr(cntrl.Name, "subject") > 0 Then
                cntrl.Value = ComboBox1.Text
                Exit For
            End If
        Next
    End Sub
    
    Private Sub UserForm_Initialize()
        ComboBox1.AddItem "mathmatics"
        ComboBox1.AddItem "social studies"
        ComboBox1.AddItem "history"
        ComboBox1.AddItem "geography"
        ComboBox1.AddItem "civics"
        ComboBox1.AddItem "ethics"
        ComboBox1.AddItem "chemistry"
        ComboBox1.AddItem "physics"
        ComboBox1.AddItem "earth science"
        ComboBox1.AddItem "arts"
        ComboBox1.AddItem "music"
    End Sub

    Comment

    • ZKAHADI
      New Member
      • Aug 2020
      • 58

      #3
      this code must be great but i am searching for something different.
      see the image
      there is a combobox and 3 text boxes
      i want select any subject from combobox and it the text will enter into first text box and same any subject selection will enter to another box.

      Comment

      • SioSio
        Contributor
        • Dec 2019
        • 272

        #4
        If you want to enter the same data in all textboxes, try removing the "exit for" statement from the sample code.

        Comment

        • ZKAHADI
          New Member
          • Aug 2020
          • 58

          #5
          no brother if a student have 3 subjects for example maths english and physics i want put these three subjects from combobox to separate textboxes. not necessary that i will put maths english physics i can put enlish physics maths or physics maths english. but i want click subject name from combobox and put data one by one into boxes

          Comment

          • SioSio
            Contributor
            • Dec 2019
            • 272

            #6
            I'm sorry, my English comprehension doesn't quite tell you what you want to do.
            Maybe you need another combobox to select a language?

            Comment

            • ZKAHADI
              New Member
              • Aug 2020
              • 58

              #7
              let me simple because i am also weak in english.

              combo box ..... English
              Physics
              Biology
              Maths
              Chemistry

              SubjectText1... ..... SubjectText2... ..... SubjectText3... ......

              i want to click and put my choice subject from combobox to SubjectText1
              then i want click combobox select my choice another subject to put in subjectText2
              same combobox select my choice subject to subjectText3

              Comment

              • SioSio
                Contributor
                • Dec 2019
                • 272

                #8
                The sample code shown at the beginning is sufficient for that requirement.

                An easier way is to make everything comboboxes instead of using text boxes.

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  1. Instead of a Combo Box, create a List Box on your Form (lstSubjects).
                  2. Set the MultiSelect Property of this List Box to Extended.
                  3. Create four Text Boxes on your Form named sequentially from Text1, Text2, Text3, and Text4.
                  4. Copy-N-Paste then following Code into the Click Event of a Command Button. It will check to see that only a Maximum of four Items in lstSubjects has been selected, then write those selections to Text1, Text2, Text3, and Text4

                  Code:
                  Dim varItm As Variant
                  Dim intCtr As Integer
                  Dim intCtr2 As Integer
                  
                  'Clear the Text Boxes
                  For intCtr2 = 1 To 4
                    Me.Controls("Text" & CStr(intCtr2)) = Null
                  Next
                  
                  If Me![lstSubjects].ItemsSelected.Count = 0 Then
                    Exit Sub
                  ElseIf Me![lstSubjects].ItemsSelected.Count > 4 Then
                    MsgBox "You can only a Maximum of 4 Subjects", vbExclamation, "Invalid Entry"
                  Else    '3 or more Items were selected
                    For Each varItm In Me![lstSubjects].ItemsSelected
                      intCtr = intCtr + 1
                      Me.Controls("Text" & CStr(intCtr)) = Me![lstSubjects].ItemData(varItm)
                    Next
                  End If

                  Comment

                  • SioSio
                    Contributor
                    • Dec 2019
                    • 272

                    #10
                    If you organize the requirements with my understanding
                    1. Empty the four text boxes.
                    2. When you select one item from combobox1, it will always be entered in SubjectText1.
                    3. Make the 1st selected item not selectable from combobox1.
                    4. When you select the 2nd item from combobox1, it will always be entered in SubjectText2.
                    5. Make the 2nd and 3rd selected items not selectable from combobox1.
                    6. When you select the 3rd item from combobox1, it will always be entered in SubjectText3.
                    7. Make the 1st, 2nd and 3rd selected items not selectable from combobox1.
                    8. When you select the 4th item from combobox1, it will be filled in SubjectText4.

                    Is it the flow of this process? If so, please point out what is different.

                    Comment

                    • SioSio
                      Contributor
                      • Dec 2019
                      • 272

                      #11
                      Hi ADezii.
                      In my environment, the code you showed didn't work, so I fixed it a bit to make it work.
                      Your idea, Listbox, will prevent it from selecting the same item.

                      Code:
                          'Dim varItm As Variant
                          Dim intCtr As Integer
                          Dim intCtr2 As Integer
                          Dim mSelectItem As Integer
                          
                          'Clear the Text Boxes
                          For intCtr2 = 1 To 4
                            Me.Controls("SubjectText" & CStr(intCtr2)) = Null
                          Next
                          
                          'Set Selected items count
                          mSelectItem = 0
                          For intCtr2 = 0 To Me![lstSubjects].ListCount - 1 '
                              If lstSubjects.Selected(intCtr2) Then
                                  mSelectItem = mSelectItem + 1
                              End If
                          Next
                           
                          'If Me![lstSubjects].ItemsSelected.Count = 0 Then
                          If mSelectItem = 0 Then
                            Exit Sub
                          'ElseIf Me![lstSubjects].ItemsSelected.Count > 4 Then
                          ElseIf mSelectItem > 4 Then
                            MsgBox "You can only a Maximum of 4 Subjects", vbExclamation, "Invalid Entry"
                          Else    '3 or more Items were selected
                            intCrt = 0
                            'For Each varItm In Me![lstSubjects].ItemsSelected
                            For intCtr2 = 0 To Me![lstSubjects].ListCount - 1
                              If Me![lstSubjects].Selected(intCtr2) Then
                                  intCtr = intCtr + 1
                                  'Me.Controls("Text" & CStr(intCtr)) = Me![lstSubjects].ItemData(varItm)
                                  Me.Controls("SubjectText" & CStr(intCtr)) = Me![lstSubjects].List(intCtr2)
                               End If
                            Next
                          End If

                      Comment

                      • ZKAHADI
                        New Member
                        • Aug 2020
                        • 58

                        #12
                        something error not working code.
                        please check below image


                        Comment

                        • SioSio
                          Contributor
                          • Dec 2019
                          • 272

                          #13
                          In my network environment, I cannot see the posted link for my security reasons.
                          Use the "Insert Image" feature in the combobox of "Post your reply".

                          Comment

                          • ZKAHADI
                            New Member
                            • Aug 2020
                            • 58

                            #14
                            image attached

                            image attached kindly find the image and solve the problem
                            Attached Files

                            Comment

                            • SioSio
                              Contributor
                              • Dec 2019
                              • 272

                              #15
                              Try it with the code shown by ADezii.
                              The operation is the same.

                              Comment

                              Working...