Solved

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

Posted on 2008-06-11
5
1,101 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

730 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