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

Listbox (Add & Removed selected items form one listbox to another listbox)

P: 22
Hi everybody,

I have two listbox in my form, the name of listbox is “ListBox1” and another is “ListBox2”. The ListBox1 Row Source Type is (Table/Query) where users will see all the employee Badge number and name. The column count is (2) and multi select is (Simple)
Now I have another ListBox which name is (ListBox2) in the same form. I also have selected the Column Count 2 and multi select is (Simple). Now my aim to create two button called (Select Employee) and (Remove Employee)

I want code behind the two buttons which move selected items form the ListBox1 to ListBox2, and similar code to remove selected items from the ListBox2.

Appreciated your help
Jan 23 '12 #1

✓ answered by ChrisPadgham

Create a button captioned Add

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAdd_Click()
  2. On Error GoTo Err_cmdAdd_Click
  3.  
  4. If Me.List1.ListIndex >= 0 Then
  5.     Me.List2.AddItem Me.List1.ItemData(Me.List1.ListIndex)
  6. End If
  7.  
  8. Exit_cmdAdd_Click:
  9.     Exit Sub
  10.  
  11. Err_cmdAdd_Click:
  12.     MsgBox Err.Description
  13.     Resume Exit_cmdAdd_Click
  14.  
  15. End Sub
  16.  
for remove

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRemove_Click()
  2. If Me.List2.ListIndex >= 0 Then
  3.     Me.List2.RemoveItem Me.List2.ListIndex
  4. End If
  5. End Sub
  6.  

Share this Question
Share on Google+
5 Replies


Seth Schrock
Expert 2.5K+
P: 2,951
Without more information, I can do little but point you in the direction I think that you need to go.

For the remove employee part... You will need to create a delete query to delete the records from the table that contains the employee information. The delete query will use the list box as the criteria for which records to delete.

For the Select employee part... What do you mean by select the employee? Clicking on the name in the list box selects it.

For a more detailed response, I need more information such as the SQL code for the row source query (in code tags [the pound sign to the left of the undo button]), which column the list boxes are bound to, and whether or not the employee badge number is the primary key. Also, your title seems to ask a different question then is asked in the text of your question. Are you trying to transfer records from one list box to another or are just trying to delete the records?
Jan 24 '12 #2

P: 22
Seth,

Thanks a lot for response. as you required, the ListBix1 is bound to "EmpT" and the second ListBox2 is not bound to anywhere. Yes, I am trying to transfer items from one list box to another list box by using pointing right and left Command buttons.
Any Help?
Thanks,
Jan 24 '12 #3

Seth Schrock
Expert 2.5K+
P: 2,951
What tables are behind the two list boxes? What are the primary key fields of those tables? Again, I need the SQL code that is the row source of the list boxes.
Jan 24 '12 #4

P: 11
Create a button captioned Add

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAdd_Click()
  2. On Error GoTo Err_cmdAdd_Click
  3.  
  4. If Me.List1.ListIndex >= 0 Then
  5.     Me.List2.AddItem Me.List1.ItemData(Me.List1.ListIndex)
  6. End If
  7.  
  8. Exit_cmdAdd_Click:
  9.     Exit Sub
  10.  
  11. Err_cmdAdd_Click:
  12.     MsgBox Err.Description
  13.     Resume Exit_cmdAdd_Click
  14.  
  15. End Sub
  16.  
for remove

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRemove_Click()
  2. If Me.List2.ListIndex >= 0 Then
  3.     Me.List2.RemoveItem Me.List2.ListIndex
  4. End If
  5. End Sub
  6.  
Jan 24 '12 #5

P: 22
Chris,

Thanks a lot, It works fine. Much appreciated

Have a great day!
Jan 26 '12 #6

Post your reply

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