"John" <Jo**@nospam.infovis.co.uk> wrote in message news:<3f*********************@news.dial.pipex.com> ...
Hi
How can I programmatically add elements to a listbox?
Thanks
Regards
John,
Below is code I have used in one of my forms for exactly this purpose
Public Sub CopyListItem(srcLBox As Control, trgLBox As Control)
'Copies selected items from one listbox to another
Dim ctl As Control
Dim varItem As Variant
Dim trgList As String
Set ctl = srcLBox
'Create a string by concatenating the selected items
For Each varItem In ctl.ItemsSelected
trgList = trgList & ctl.ItemData(varItem) & ";"
Next varItem
For Each varItem In ctl.ItemsSelected
Next varItem
'Set the string as rowsource for the target listbox
trgLBox.RowSource = trgList
trgLBox.SetFocus
'Deselect the items in the source list box
Dim intCurrentRow As Integer
For intCurrentRow = 0 To ctl.ListCount - 1
If ctl.Selected(intCurrentRow) Then
ctl.Selected(intCurrentRow) = 0
End If
Next intCurrentRow
End Sub