Solved

Move data from one list to another and back again

Posted on 2012-03-16
6
310 Views
Last Modified: 2012-03-16
I have to list boxes, the user double-clicks to select an employee and this name shows up in the second list.  This works fine - HOWEVER, if the user needs to remove a name from the second list box (by double-clicking) , the name should move back to the first list box.  I have the code that moves the data from lstEmployees to lstAssignedTo, but if the user changes their mind, not sure how to do it in reverse.  The code uses a txtSelection control on the form to hold the userID's of those that have been selected, which is used in the underlying query to update the rowsource.

Sandra

Private Sub lstEmployees_DblClick(Cancel As Integer)
Dim strList As Variant
Dim ctl As Control
Dim varItem As Variant

    Set ctl = Me.lstEmployees

    strList = Me.txtSelection
   
    For Each varItem In ctl.ItemsSelected
        If IsNull(Me.txtSelection) Then
            strList = """" & ctl.ItemData(varItem) & ""","""
        Else
            strList = strList & """" & ctl.ItemData(varItem) & """" & ","""
        End If
    Next varItem
   
    Me.txtSelection = Left(strList, Len(strList) - 1)
   
    Call FctRefreshLists

End Sub
Public Function FctRefreshLists()
'The list box on the right will show only all items selected
On Error GoTo ErrorHandler
Dim strSelected As String
Dim strNotSelected As String
Dim intUserTeam As Integer
Dim strSQLNotselected As String
Dim strSelectedText As String

strSelectedText = Left(Me.txtSelection, Len(Me.txtSelection) - 1)

strSelected = ""

If Not IsNull(Me.txtSelection) And Me.txtSelection <> "" Then
    strSelected = "SELECT tblEmployees.EmpUserID, [EmpLastName] & ', ' & [EmpFirstName] AS EmpName " & _
        "FROM tblEmployees WHERE tblEmployees.EmpTerminated = 0 AND EmpUserID In(" & strSelectedText & ") " & _
        "ORDER BY tblEmployees.EmpLastName "
Else 'don't show any
    strSelected = ""
End If

Me.lstAssignedTo.RowSource = strSelected
Me.lstAssignedTo.Requery

'The list box on the LEFT will show only all items that are NOT selected
If Not IsNull(Me.txtSelection) And Me.txtSelection <> "" Then
    strNotSelected = "SELECT tblEmployees.EmpUserID, [EmpLastName] & ', ' & [EmpFirstName] AS EmpName " & _
        "FROM tblEmployees WHERE tblEmployees.EmpTerminated = 0 AND EmpUserID NOT IN(" & strSelectedText & ") " & _
        "ORDER BY tblEmployees.EmpLastName "
Else
    strNotSelected = "SELECT tblEmployees.EmpUserID, [EmpLastName] & ', ' & [EmpFirstName] AS EmpName " & _
        "FROM tblEmployees WHERE tblEmployees.EmpTerminated = 0" & _
        "ORDER BY tblEmployees.EmpLastName "
End If

Me.lstEmployees.RowSource = strNotSelected
Me.lstEmployees.Requery

Exit_ErrorHandler:
    Exit Function
ErrorHandler:
    MsgBox "Error Number: " & Err.Number & vbCrLf & _
           "Description: " & Err.description & vbCrLf & _
           "Function FctRefreshLists", vbOKOnly
    Resume Exit_ErrorHandler
End Function
0
Comment
Question by:ssmith94015
[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
  • 3
6 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37730872
See here perhaps:
http://support.microsoft.com/kb/132137

I have a sample that does this, I'll try to post it tonight.
But I am sure another expert will be along to help you with this directly
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 37730883
0
 

Author Comment

by:ssmith94015
ID: 37730916
boag2000 Thank you.  I took a look at the article and it is a different approach, but am going to try some variations of it.  I am looking forward to your sample.

Sandra
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:ssmith94015
ID: 37730922
It looks perfect.  However, what libraries does it need as I need to create some references for it to work.

Sadnra
0
 

Author Closing Comment

by:ssmith94015
ID: 37730928
Got it to work, I just needed the script runtime.  Thank you again, this is saving me a big chunk of time.  Your layout is much better than teh approach I was going with
Sandra
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37730955
You should not have needed any special libraries to run my sample.
Perhaps the app you are using this in does...?

In any event, I am glad it is working for you.

Enjoy the weekend...
;-)

Jeff
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

732 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