Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-11
5
Medium Priority
?
1,216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
koutny earned 1000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

636 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