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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying values from one List box to another

Status
Not open for further replies.

Zirak

MIS
Feb 3, 2003
164
0
0
US
Hi,
Issue:
I have two list boxes on a form.
I want to copy items that are selected from the first one (MListSource) to the second one (MListFinal).


Problem
I am able to only copy the value from the "Bound" clomun of my first list box, but my list boxes have two columns "ID" and "Name" and I need to copy an array to the other list box.

Do you know how?



Here is the code to copy the bound column:
----------------------------------------------------
Dim MListSource As ListBox, MListFinal As ListBox
Dim itm As Variant

Set MListSource = Me!MListSource
Set MListFinal = Me!MListFinal
' Check selected items.
For Each itm In MListSource.ItemsSelected
' Set RowSource property for first selected item.
If MListFinal.RowSource = "" Then
MListFinal.RowSource = MListSource.ItemData(itm)
Else
' Check whether item has already been copied.
If Not InStr(MListFinal.RowSource, MListSource.ItemData(itm)) > 0 Then
MListFinal.RowSource = MListFinal.RowSource & ";" & MListSource.ItemData(itm)
End If
End If
Next itm
 
Zirak
You can use the explicit command:
MListSource.Column(Column,Row)
OR default to MListSource.Column(Column) when a record is selected (row implicitely selected)

If you are using multiselect you will also need to loop through the ItemsSelected property to identify which items were selected.
Regards,
John
 
Thanks for your reply,
I tried the following
--------
Dim VarItem As Variant
Dim ID As Long
Dim Name As Long

For Each VarItem In Me!MListSource.ItemsSelected
ID = Me!MListSource.ItemData(VarItem)
Name = Me!MListSource.Column(1, VarItem)

Me!MListFinal.AddItem ID & ";" & Name
Next VarItem

-----

Problem:
If the Name string has a "," character the Additem would add it characters after "," to the next row and it will mess up every thing, for example:


Source List > Final List
------------------ ------------------------
1 Andy Smith 1 Andy Smith
2 Joe Smith,CPA 2 Joe Smith
3 Bill Gates CPA 3
Bill Gates



any idea how to fix this?
the only thing I can think of is replacing the "," characters, but there should be some other way.

Thanks
 
>>PLease disgard the previous message
Thanks for your reply,
I tried the following
--------
Dim VarItem As Variant
Dim ID As String
Dim Name As String
For Each VarItem In Me!MListSource.ItemsSelected
ID = Me!MListSource.ItemData(VarItem)
Name = Me!MListSource.Column(1, VarItem)

Me!MListFinal.AddItem ID & ";" & Name
Next VarItem

-----

Problem:
If the Name string has a "," character the Additem would add it characters after "," to the next row and it will mess up every thing, for example:


Source List > Final List
------------------ ------------------------
1 Andy Smith 1 Andy Smith
2 Joe Smith,CPA 2 Joe Smith
3 Bill Gates CPA 3
Bill Gates



any idea how to fix this?
the only thing I can think of is replacing the "," characters, but there should be some other way.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top