Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox to Listbox 1

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I have two listboxes, the first being multiselect. I am trying to:

1) Put the selected item from the first listbox into the second listbox.

2) Remove the item from the second listbox if the item gets unselected from the first list.

I don't want an array, as the second list has the option for row sorting order which would get killed if it had an array passed over from the first list if after sorting list 2 another item were selected on list 1. How do I do it? Thanks
 
How are ya Domino2 . . .

Post the [blue]Row Source[/blue] of the listboxes.
[ol][li]Is it your intent to [blue]move or copy[/blue] to the 2nd listbox?[/li]
[li]I expect on opening the form the 2nd listbox is empty. Is this correct?[/li]
[li]Approximately how many records (max) will comprise the recordset of 1st listbox?[/li]
[li]Identify the [blue]field names[/blue] of the data to transfer from 1st listbox to 2nd.[/li][/ol]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks. Listbox1 contains fieldnames from a table

Dim str2 As String
Dim f As Field
Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("Buildings")

For Each f In rs.Fields
str2 = str2 & f.Name & ";"
Next

Me.ListBox1.RowSource = str2

str2 = ""
Set rs = Nothing

The first list is a mulltiselect list

The second listbox opens empty

I am trying to pass selected items from the first list to the second one. However, during the operation the user may sort the order of the fieldnames in list 2.

Problem is if further items are selected in the first list, then I do not want to upset the row order of the second list.

The number of fieldnames will not exceed 30

Thanks again


 
You can set the Row Source type of your field list box to "Field List" and its Row Source to "Buildings". Then use this code to update the Row Source of the selected fields list box.

Code:
Private Sub lboFieldList_AfterUpdate()
    Dim strSelected As String
    Dim item
    For Each item In Me.lboFieldList.ItemsSelected
        strSelected = strSelected & Me.lboFieldList.ItemData(item) & ";"
    Next
    If Len(strSelected) > 0 Then
        strSelected = Left(strSelected, Len(strSelected) - 1)
    End If
    Me.lboSelectedFields.RowSource = strSelected
End Sub

Duane
Hook'D on Access
MS Access MVP
 
Domino2 . . .

I have code prepared but need to know if you intend to [blue]move or copy[/blue] the selections to lixtbox2?

Also, do you have [blue]anything which indicates the current sort order?[/blue] . . . If not you'll need one. Perhaps a variable in the declarations section of the forms code module. The variable can be updated when the user sorts.


See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks. Basically List1 fills with all field names. The user selects what they want from the list and these go into List2. List2 has the facility of moving rows up and down in a final order. However there is a sitation where after arranging the rows order in List2, the user might select another fieldname from List1. If this happens it must go to the bottom of list2, and not affect the order they have been sorted in.

Finaly when List2 has been built, there is an export button which goes through the database in order of List2, exporting the data related to the fieldnames. The export function is to export in XML format.

Thats about it, but its been giving me grief.

Thanks also to dhookom, not had a chance to see if applicable.
 
Domino2 . . .
[blue] ... after arranging the rows order in List2, the user might select another fieldname from List1. [purple]If this happens it must go to the bottom of list2[/purple], and not affect the order they have been sorted in.[/blue]
OK ... in the code module of the form, copy/paste the following routine:
Code:
[blue]Private Sub XferTo2nd()
   Dim LBx1 As ListBox, LBx2 As ListBox, Src1 As String, idx, Build As String
   
   Set LBx1 = Me!List130
   Set LBx2 = Me!List132
   Src1 = LBx1.RowSource
   
   If LBx1.ItemsSelected.Count <> 0 Then
      [green]'Pack selections[/green]
      For Each idx In LBx1.ItemsSelected
         If Build <> "" Then
            Build = Build & ";" & LBx1.ItemData(idx)
         Else
            Build = LBx1.ItemData(idx)
         End If
         
         [green]'Remove selected items from rowsource of listbox1
         'Rem out the following three lines if you want to copy instead![/green]
         Src1 = Replace(Src1, LBx1.ItemData(idx), "")
         Src1 = Replace(Src1, ";;", ";")
         If Left(Src1, 1) = ";" Then Src1 = Right(Src1, Len(Src1) - 1)
      Next
      
      [green]'If appending, names go to the bottom of the list[/green]
      If LBx2.RowSource <> "" Then Build = LBx2.RowSource & ";" & Build
      
      LBx1.RowSource = Src1
      LBx2.RowSource = Build
   Else
      MsgBox "No Selections Made!"
   End If
   
End Sub[/blue]
In your code, just call the routine after selections are made. Using a button the code would be:
Code:
[blue]   Call XferTo2nd[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Woops! [blush] . . .
Code:
[blue]Change:
   Set LBx1 = Me!List130
   Set LBx2 = Me!List132

To:
   Set LBx1 = Me![purple][B][I]ListboxName1[/I][/B][/purple]
   Set LBx2 = Me![purple][B][I]ListboxName2[/I][/B][/purple][/blue]
[blue]You![/blue] substitute proper names in [purple]purple.[/purple]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
faq702-6326

Drop the class in a instantiate the objects and your done.
 
TheAceman1
Thanks for all your effort, I got that up and running like a dream. Thanks for putting in time to solve it!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top