Full Version: Removing selected item from a "ValueList" listbox
UtterAccess Forums > Microsoft® Access > Access Forms
dice
Hi there
Is there syntax to remove items from a valuelist listbox?? i like to be able to allow users to select an item from the form's listbox and click a button to remove that selected item. i've research this site for any post with similar problem but always gotten the listbox rowsource to be a SELECT statement or referencing a table...
Listbox rowsource = "Country";"CC";"Breakouts";"Region"
Any directions or advise appreciated and thanks in advance... notworthy.gif
Larry Larsen
Hi
Try this:
!--c1-->
CODE
Dim ingRow As Integer
ingRow = Me.YourComboName.ListIndex
If ingRow >= 0 Then
Me.YourComboName.RemoveItem ingRow
End If
Me.YourComboName = ""

This should remove an item during the running of the form/combo, if the form is re-opened the list is retuned back to it's orginal list..
Anythink like what your after..?
HTH's
thumbup.gif
dice
Hi Larry
My Access 2K does not seem to like the syntax "RemoveItem" and i'm getting a "Method or Data Member not found". Hope u can help again...
Larry Larsen
Hi
Strange I used A2K to buld the code, have you checked your reference libaray for "Missing" links.

Also try and write the code in rather than copy & paste and see if it allows the "RemoveItem" option.
thumbup.gif
Edited by: Larry Larsen on Mon Oct 4 1:50:56 EDT 2004.
dice
i did rewrite the codes and also checked the reference in my A2K but all seem to be ok... i ran the repair and compact as well as compile the code with and w/o the RemoveItem syntax, the only time i get a compile error is if i did not remark that particular syntax...
I have attached my reference, hmmm... hope its jus a reference i did not tick.
Larry Larsen
Hi Darryl
I'm at a work location are unable to access attachment's but I should back at home in a couple of hours.
lso this machine is completely different to my own..
thumbup.gif
Larry Larsen
Hi
Just in the mean time can you post up your code..?
thumbup.gif
dice
Hi Larry
Thanks for the helping hand, here is the codes on my button onclick event:
CODE
Private Sub cmdRemoveF_Click()
    Dim ingRow As Integer
    
    If Me.lstAddedFilters.ItemsSelected.Count = 0 Then
        MsgBox "Please select filter to be removed.", _
                vbInformation, "Filter Not Selected"
        Me.lstAddedFilters.SetFocus
        Exit Sub
    End If
    ingRow = Me.lstAddedFilters.ListIndex
    If ingRow >= 0 Then
        Me.lstAddedFilters.RemoveItem ingRow
    End If
End Sub

frown.gif i have adapted your codes to a listbox instead, though this problem (.RemoveItem) persist for a combobox too. Thanks again.
RoyVidar
I think the .AddItem and .RemoveItem methods of combos and lists became avvailable in xp (2002) - I'm not sure, they could of course have become available in a late 2000 SP?. To remove and add items in prior versions, one would need to either repopulate the list, without the selected item(s), or try to remove items from the rowsource string thru string manipulation.
Larry Larsen
Hi
Yep.. Just tried at home and "no" go.. they must have A202 at the work location (and I never noticed)..
duh.
thumbup.gif
dice
Thanks then to both Larry o! & Roy o! ... i'll try something else
Chaga
You can try something like that.
Private Sub cmdRemoveF_Click()
Dim strRow As String
Dim strSel As String
If Me.lstFilter.ItemsSelected.Count = 0 Then
MsgBox "Ding"
Me.lstFilter.SetFocus
Exit Sub
Else
If Right(Me.lstFilter.RowSource, 1) <> Chr(59) Then
strRow = Me.lstFilter.RowSource & Chr(59)
Else
strRow = Me.lstFilter.RowSource
End If
strSel = Me.lstFilter & Chr(59)
strRow = Replace(strRow, strSel, "")
strRow = Replace(strRow, Chr(34), "")
Me.lstFilter.RowSource = strRow
Me.lstFilter.Tag = strRow
Me.lstFilter.Requery
End If
End Sub
/pre>
Oknow it's ugly grin.gif, but I think it should work.
PS: The rowsource is stored in the Tag of the control for future usage.
HTH
Chaga
Oops, noticed a glitch,
Here's the new code.
Private Sub cmdRemoveF_Click()
Dim strRow As String
Dim strSel As String
If Me.lstFilter.ItemsSelected.Count = 0 Then
MsgBox "Ding"
Me.lstFilter.SetFocus
Exit Sub
Else
If Right(Me.lstFilter.RowSource, 1) <> Chr(59) Then
strRow = Replace(Me.lstFilter.RowSource, Chr(34), "") & Chr(59) 'New Line
Else
strRow = Replace(Me.lstFilter.RowSource, Chr(34), "") 'New Line
End If
strSel = Me.lstFilter & Chr(59)
strRow = Replace(strRow, strSel, "")
' strRow = Replace(strRow, Chr(34), "")
Me.lstFilter.RowSource = strRow
Me.lstFilter.Tag = strRow
Me.lstFilter.Requery
End If
End Sub

Sorry blush.gif
dice
No apologies pls... frown.gif
its wonderful to receive help and will give it a shot when back in office... Thanks.
yayhandclap.gif
dice
Hullo Charles, it worked like a charm, i have mod it to remove items in a 3-column listbox... Thanks very much.
Chaga
Great frown.gif
You're welcome.
Larry Larsen
Hi Charles
Nice little feature.. notworthy.gif
thumbup.gif
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.