Connecting Tech Pros Worldwide Help | Site Map

Double-clicking to move an item between list boxes

  #1  
Old May 1st, 2008, 09:43 PM
zepphead80's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: New York City
Posts: 136
Sometimes you might have two list boxes side by side, and want to double-click on an item in one box to move it to the other.

For instance, maybe the first list box contains a list of customers, and you want to perform some function (say, email them) that pertains to only a few of those customers. By double-clicking on the customer names you want, you get the desired list in the right hand box.

First, make a generic sub that takes two listbox arguments:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub MoveListItem(lstFrom As ListBox, lstTo As ListBox)
  3.      lstTo.AddItem (lstFrom.Value)
  4.      lstFrom.RemoveItem (lstFrom.Value)
  5. End Sub
  6.  
Then call this sub in the double-click events for the respective list boxes:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub lstA_DblClick(Cancel As Integer)
  3.     MoveListItem Me.lstA, Me.lstB
  4. End Sub 
  5.  
  6. Private Sub lstB_DblClick(Cancel As Integer)
  7.     MoveListItem Me.lstB, Me.lstA
  8. End Sub 
  9.  
Here you'll replace "lstA" and "lstB" with whatever the names of your list boxes are. You could modify this slightly by having a button situated between the boxes, and move the item from one to the other when the button is clicked.

It's a simple task but can come in handy!

Pat



  #2  
Old May 28th, 2008, 01:50 AM
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,781

re: Double-clicking to move an item between list boxes


Pat

Does this belong in VB. I don't think you can use .AddItem and .RemoveItem in VBA without at least adding a library.

If it needs to be moved just post a comment here and I'll take care of it.

Mary
  #3  
Old June 10th, 2008, 04:02 PM
zepphead80's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: New York City
Posts: 136

re: Double-clicking to move an item between list boxes


Quote:
Originally Posted by msquared
Pat

Does this belong in VB. I don't think you can use .AddItem and .RemoveItem in VBA without at least adding a library.

If it needs to be moved just post a comment here and I'll take care of it.

Mary
I didn't have any reference issues when using this code in VB 6 (Access). Do you want me to post which references I have enabled just to be sure?

Thanks!

Pat
  #4  
Old April 15th, 2009, 04:33 AM
JustJim's Avatar
Expert
 
Join Date: May 2007
Location: Bacchus Marsh, Victoria, Australia
Posts: 406

re: Double-clicking to move an item between list boxes


Quote:
Originally Posted by zepphead80 View Post
I didn't have any reference issues when using this code in VB 6 (Access). Do you want me to post which references I have enabled just to be sure?

Thanks!

Pat
Hi Pat,

This is simple and elegant but, sadly errors out for me. Perhaps a library problem as Mary thought. I tried it at home using Access '03/VBA Retail 6.4.8869 with the following references included:

Visual Basic for Applications
Microsoft Access 11.0
Microsoft DAO 3.6
Microsoft ActiveX Data Objects 2.1
OLE Automation

Anything else you have loaded that might be relevant?

The error is 94: Invalid use of Null, and is on .AddItem line of the MoveListItem sub. A little hovering tells me that ListFrom.Value is null as is Me.lstA and Me.lstB from the calling subs.

Also, the helpfile for .AddItem and .RemoveItem states that the list box RowSourceType must be "Value List", which means you would have to build the contents of the first listbox by code or hard key it when you design the form. A pain, but a mild one.

Jim

Last edited by JustJim; April 15th, 2009 at 04:50 AM. Reason: To include the error!
  #5  
Old April 15th, 2009, 05:07 AM
JustJim's Avatar
Expert
 
Join Date: May 2007
Location: Bacchus Marsh, Victoria, Australia
Posts: 406

re: Double-clicking to move an item between list boxes


Sorry, ignore almost all of the above - I had one of the list boxes set to Extended Selection and that messed it up for some reason.

It now works fine with the basic libraries above, thank you.

The comment about the RowSourceType should be noted by others though.

(Blushing)Jim
  #6  
Old April 15th, 2009, 02:06 PM
zepphead80's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: New York City
Posts: 136

re: Double-clicking to move an item between list boxes


Glad that my post was of assistance to you! No need to blush - show me someone who doesn't make these kinds of mistakes when programming in Access!

Pat
  #7  
Old April 16th, 2009, 08:29 AM
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,781

re: Double-clicking to move an item between list boxes


Hi Pat

Haven't seen this thread in a while. My misinformation on .AddItem is probably a holdover from some of those earlier versions of Access libraries. I haven't tried to use it in years. I'll have to start experimenting again.

Thanks for the information. I always learn something new from the bytes experts no matter how much I think I know. It's why I love information sharing sites like this. Stops me from getting stuck in old patterns.

Mary
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Laws and Myths of Usability & Interface Design YellowfinTeam answers 1 December 12th, 2006 04:35 PM
Laws and Myths of Usability & Interface Design YellowFin Announcements answers 0 December 12th, 2006 10:55 AM
Dynamic Crosstab Report Nathan Bloomfield answers 1 November 12th, 2005 09:10 PM
Listbox Order Bill answers 8 November 12th, 2005 03:18 PM