Solved

Move data from one list to another and back again

Posted on 2012-03-16
6
302 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
  • 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now