By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,119 Members | 614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,119 IT Pros & Developers. It's quick & easy.

Selection from combobox and populate multiple fields by separate clicks

P: 34
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 ?
2 Weeks Ago #1

✓ answered by ADezii

I created a simple Demo for you that will hopefully explain everything in detail.

Share this Question
Share on Google+
21 Replies

100+
P: 191
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub ComboBox1_Change()
  2.     For Each cntrl In UserForm1.Controls
  3.         If cntrl.Value = "" And InStr(cntrl.Name, "subject") > 0 Then
  4.             cntrl.Value = ComboBox1.Text
  5.             Exit For
  6.         End If
  7.     Next
  8. End Sub
  9.  
  10. Private Sub UserForm_Initialize()
  11.     ComboBox1.AddItem "mathmatics"
  12.     ComboBox1.AddItem "social studies"
  13.     ComboBox1.AddItem "history"
  14.     ComboBox1.AddItem "geography"
  15.     ComboBox1.AddItem "civics"
  16.     ComboBox1.AddItem "ethics"
  17.     ComboBox1.AddItem "chemistry"
  18.     ComboBox1.AddItem "physics"
  19.     ComboBox1.AddItem "earth science"
  20.     ComboBox1.AddItem "arts"
  21.     ComboBox1.AddItem "music"
  22. End Sub
2 Weeks Ago #2

P: 34
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.

2 Weeks Ago #3

100+
P: 191
If you want to enter the same data in all textboxes, try removing the "exit for" statement from the sample code.
2 Weeks Ago #4

P: 34
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
2 Weeks Ago #5

100+
P: 191
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?
2 Weeks Ago #6

P: 34
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
2 Weeks Ago #7

100+
P: 191
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.
2 Weeks Ago #8

ADezii
Expert 5K+
P: 8,748
  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
Expand|Select|Wrap|Line Numbers
  1. Dim varItm As Variant
  2. Dim intCtr As Integer
  3. Dim intCtr2 As Integer
  4.  
  5. 'Clear the Text Boxes
  6. For intCtr2 = 1 To 4
  7.   Me.Controls("Text" & CStr(intCtr2)) = Null
  8. Next
  9.  
  10. If Me![lstSubjects].ItemsSelected.Count = 0 Then
  11.   Exit Sub
  12. ElseIf Me![lstSubjects].ItemsSelected.Count > 4 Then
  13.   MsgBox "You can only a Maximum of 4 Subjects", vbExclamation, "Invalid Entry"
  14. Else    '3 or more Items were selected
  15.   For Each varItm In Me![lstSubjects].ItemsSelected
  16.     intCtr = intCtr + 1
  17.     Me.Controls("Text" & CStr(intCtr)) = Me![lstSubjects].ItemData(varItm)
  18.   Next
  19. End If
2 Weeks Ago #9

100+
P: 191
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.
1 Week Ago #10

100+
P: 191
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.

Expand|Select|Wrap|Line Numbers
  1.     'Dim varItm As Variant
  2.     Dim intCtr As Integer
  3.     Dim intCtr2 As Integer
  4.     Dim mSelectItem As Integer
  5.  
  6.     'Clear the Text Boxes
  7.     For intCtr2 = 1 To 4
  8.       Me.Controls("SubjectText" & CStr(intCtr2)) = Null
  9.     Next
  10.  
  11.     'Set Selected items count
  12.     mSelectItem = 0
  13.     For intCtr2 = 0 To Me![lstSubjects].ListCount - 1 '
  14.         If lstSubjects.Selected(intCtr2) Then
  15.             mSelectItem = mSelectItem + 1
  16.         End If
  17.     Next
  18.  
  19.     'If Me![lstSubjects].ItemsSelected.Count = 0 Then
  20.     If mSelectItem = 0 Then
  21.       Exit Sub
  22.     'ElseIf Me![lstSubjects].ItemsSelected.Count > 4 Then
  23.     ElseIf mSelectItem > 4 Then
  24.       MsgBox "You can only a Maximum of 4 Subjects", vbExclamation, "Invalid Entry"
  25.     Else    '3 or more Items were selected
  26.       intCrt = 0
  27.       'For Each varItm In Me![lstSubjects].ItemsSelected
  28.       For intCtr2 = 0 To Me![lstSubjects].ListCount - 1
  29.         If Me![lstSubjects].Selected(intCtr2) Then
  30.             intCtr = intCtr + 1
  31.             'Me.Controls("Text" & CStr(intCtr)) = Me![lstSubjects].ItemData(varItm)
  32.             Me.Controls("SubjectText" & CStr(intCtr)) = Me![lstSubjects].List(intCtr2)
  33.          End If
  34.       Next
  35.     End If
1 Week Ago #11

P: 34
something error not working code.
please check below image


https://i.imgur.com/B7PIZjQ.jpg
1 Week Ago #12

100+
P: 191
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".
1 Week Ago #13

P: 34
image attached kindly find the image and solve the problem
Attached Images
File Type: jpg ed.jpg (98.2 KB, 6 views)
1 Week Ago #14

100+
P: 191
Try it with the code shown by ADezii.
The operation is the same.
1 Week Ago #15

P: 34
The code is not working or i am not understanding to use it. could your please make a form and upload for me? please
1 Week Ago #16

100+
P: 191
I will explain with the code of ADezii.
The names of the TextBox are "Text1", "Text2", "Text3", and "Text4", respectively.
The name of the ListBox is "lstSubjects".
Set "MultiSelect Property" of ListBox (lstSubjects) to Extended.
If you want to select multiple items from the Listbox, hold down the Shift key while using the mouse.
1 Week Ago #17

P: 34
i did what you said but on clicking button nothing happen not even error but no data putting in boxes.. look at image please
Attached Images
File Type: jpg itled.jpg (77.3 KB, 7 views)
1 Week Ago #18

P: 1
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. mobdro apk tubemate
1 Week Ago #19

100+
P: 191
Looking at the image file of post # 14, is the name of the "Add" button Command23?

I think the position to write the code is correct.

If that doesn't work, try setting a breakpoint on any line (for example, line 6) and using the F8 key to run it line by line.
1 Week Ago #20

ADezii
Expert 5K+
P: 8,748
I'll Upload a Demo sometime today which will make things much clearer for you.
1 Week Ago #21

ADezii
Expert 5K+
P: 8,748
I created a simple Demo for you that will hopefully explain everything in detail.
Attached Files
File Type: zip Subjects.zip (23.3 KB, 10 views)
1 Week Ago #22

Post your reply

Sign in to post your reply or Sign up for a free account.