Unbound List Box Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Real simple question more than likely. I have searched the forums but can't
seem to find where this has been answered before so here goes...

I am doing a mock-up for a possible application. I have a simple form with
two list boxes and two command buttons.

The first box contains records based on a drop down combo box's value.
Everything works fine with List Box 1.

List box 2 is the problem. I was able to use the following to add values
from list box 1 with no problem:

Private Sub cmdAdd_Detail_Click()

Dim lst1 As ListBox, lst2 As ListBox
Dim strSQL As String, strMsg As String
Dim itm As Variant

Set lst1 = Me.LstAvailable
Set lst2 = Me.lstDetail


lst2.RowSourceType = "Value List"

' Check selected items.
For Each itm In lst1.ItemsSelected

' Set RowSource property for first selected item.

If lst2.RowSource = "" Then
lst2.RowSource = lst1.ItemData(itm)
Else

' Check whether item has already been copied.

If Not InStr(lst2.RowSource, lst1.ItemData(itm)) > 0 Then
lst2.RowSource = lst2.RowSource & ";" & lst1.ItemData(itm)
End If

End If

Next itm

End Sub

That all works perfectly as I would like it to. The problem is... that the
other command button I have is to remove an item from list 2 after the user
decides they dont want it anymore. (or multiple items). So... I am kind of
confused as to how go about this. I thought it would be something simple
like:

List2.rowsource = list2.rowsource - list2.itemdata(itm)

but as I am sure you are aware.. that didn't work.

Any help is greatly appreciated and I am sure it is a very simple solution I
have just overlooked.

Thanks in advance.
 
Your rowsource is just a string. So you'll have to search
the string for the item, remove it, remove a semicolon,
put the string back together and re-assign it. You could
store your items in an array, but you still have to look
for the unwanted item in the array and so on.
Geof.
 
Thanks Geof. Do you have a link to an example of doing this within the forms
code, such as on the buttons click execution?

Again, greatly appreciated.
 
I was writing a rather messy sub when I realized that it
was going to get messier because of the semicolons. Then I
had a brain wave. It sounds like you know how to loop
through the list items.
Do that, building a string of items that AREN'T selected,
with the semicolons of course, then assign that string to
the row source and requery.
Geof.
 
Back
Top