Code Builder for simple copy/paste

mriggio

Board Regular
Joined
Jul 28, 2002
Messages
54
I have a form with the following:
-A List Box (with hundreds of names) looking up values from a Table.
-Two buttons
-A Second List Box (also with names) looking up values from a Query.
I need help coding a macro for the two buttons. I would like to buttons to do the following:
- Button 1: When a name is highlighted in the first List Box, clicking on the button will copy the name and paste it into the second list box (or move it to another Table so it will show up in the ListBox Query).
- Button 2: The same as Button 1, except the other way around; clicking the button will take the highlighted name from the second List Box and put it back in the first List Box.

Any Ideas?

Thank you in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi!

Here's my ideas:
Add a field(yes/no), I'll call it 'checked', to the master name table. Set its default value to 'false'. Create two select querries, one with criteria checked=True and one with criteria checked=False.
Create a new form. Insert 2 subforms into it. Link one subform to the 'checked' query, and one to the 'not checked' query. Insert a listbox, and set the listbox up to choose a record based on your selection, and also have the wizard look up the listboxes values from the query that the subform is linked to. Now insert a checkbox(1). Name it and have it's control source set to the new field 'checked'. Right click on it and set its visible property to false. Finally, insert a button and don't have it do anything. Right click on the button, build event, and go to the code builder. Add an event like:
___________________________
Button1_Click()
Checkbox1 = True
DoCmd.Requery
End Sub
___________________________

Now, when you select the name from the list, you go to that record in that subform, and then the button changes the checkbox from false to true, and then requeries the list so the query is refreshed and the name should no longer be on the list.

In the other subform, insert another listbox, and have it pick a record based on your selection, and have it get it's values from the other query, the one with the criteria set to 'true' for the new 'checked' field. Add a checkbox(2) and set its control source to the new field 'checked'. Add another button. Attach code the the button click event as described above and add this code:
__________________________
Button2_Click()
Checkbox2 = False
DoCmd.Requery
__________________________
The same process listed above applies to this subform as well.

If your listboxes fail to update correctly, replace DoCmd.Requery with Listbox1.Requery.

HTH,
 
Upvote 0

Forum statistics

Threads
1,221,501
Messages
6,160,175
Members
451,629
Latest member
MNexcelguy19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top