Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Move data from one list to another and back again

Posted on 2012-03-16
6
Medium Priority
?
317 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 2000 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
Industry Leaders: 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!

 

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

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

972 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