Solved

Need to move selected items from one list box to another. VBA

Posted on 2008-06-11
5
1,090 Views
Last Modified: 2013-11-28
I have an unbound list box that was populated by an ado recordset, then disconnected. When a row is selected, I would like the user to click an arrow button to apparently move the selected item to another list box. I would like another arrow button that moves the item back to the original list box. The second list box would then show which items have been selected.

I did this 10 years ago in Access, but have forgotten how. Any references would be helpful.

Thanks,
0
Comment
Question by:Dovberman
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
koutny earned 250 total points
ID: 21759169
Assuming both list boxes are unbound and there is only one column
Private Sub cmdSelect_Click()
Dim vSelectedValue As Variant
    vSelectedValue = lstBox1
    If IsNull(vSelectedValue) Or IsEmpty(vSelectedValue) Then
        MsgBox "Select an item first", vbCritical, "No item selected"
        Exit Sub
    End If
    
    lstBox2.AddItem vSelectedValue
    lstBox1.RemoveItem lstBox1.ListIndex
End Sub
 
 
 
Private Sub cmdSelect_Click()
Dim vSelectedValue As Variant
    vSelectedValue = lstBox2
    If IsNull(vSelectedValue) Or IsEmpty(vSelectedValue) Then
        MsgBox "Select an item first", vbCritical, "No item selected"
        Exit Sub
    End If
    
    lstBox1.AddItem vSelectedValue
    lstBox2.RemoveItem lstBox2.ListIndex
End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:koutny
ID: 21759175
Oops, the second procedure should have been cmdDeselect_click. But I am sure you get the drift
0
 

Author Comment

by:Dovberman
ID: 21761257
Your example refreshed my memory.

I changed the list box row source type to value list.
Add Item for multiple columns was tricky.
The column string value cannot contain embedded commas.
Column headers must be set to no. Otherwise replaced items will go into the header row.

Thanks,
0
 

Author Closing Comment

by:Dovberman
ID: 31466211
Here is the actual code:

Private Sub cmdSelectViol_Click()
    'Selects a violation from the general list
    'And moves the item to the selected list
   
    Dim intViolationID As Integer
    Dim strViolationDesc As String
    Dim strAuth As String
     
    On Error GoTo cmdSelectViol_Click_Err
   
    If IsNull(lstGenViolations.Column(0)) Or IsEmpty(lstGenViolations.Column(0)) Then
        MsgBox "Select an item first", vbCritical, "No item selected"
        Exit Sub
    End If
   
    intViolationID = lstGenViolations.Column(0)
    strViolationDesc = Replace(lstGenViolations.Column(1), ",", " ") 'Remove embedded commas
    strAuth = lstGenViolations.Column(2)
   
    If IsNull(lstGenViolations.Column(2)) Then
        lstSelectedViol.AddItem Item:=intViolationID & ";" & strViolationDesc
    Else
        lstSelectedViol.AddItem Item:=intViolationID & ";" & strViolationDesc & ";" & strAuth
    End If '!ViolationAuth IsNull
   
    lstGenViolations.RemoveItem lstGenViolations.ListIndex
   
cmdSelectViol_Click_Exit:

    Exit Sub
 
cmdSelectViol_Click_Err:
    LogError Err.Number, Err.Description, "Form: " & Me.Name & ", cmdSelectViol_Click "
    DoCmd.Hourglass False
    Resume cmdSelectViol_Click_Exit

 End Sub
0
 
LVL 12

Expert Comment

by:koutny
ID: 21761360
Yes, I knew I was being lazy when I assumed there was only one column...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question