Deleting from a list box

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I have a main form for typical demographics and a second
form opened by a button for donations to a charity. On
the second form is a calendar and a calculator for entry
of new donations and a listbox to display all previous
donations for that person. The listbox is controlled by a
query looking through the Contributions table for every
donation from the person pointed to by the main table. I
can add donations, but when I click on the delete button
with a line in the list box highlighted it asks whether I
want to delete 1 record and then does nothing. How do I
delete data in the listbox? Thanks.

Dave
 
You will need a unique value for the record included in the listbox in a
hidden column (width=0). Take the value of this column and use it in a
delete query to delete the value from the table that the listbox's row
source is based on then requery the listbox.

Example
Dim strSQL As String
strSQL = "Delete * From tblDonations Where [DonationID]=" &
Me.lstMyListbox.Column(0)
CurrentDb.Execute strSQL, dbFailOnError
Me.lstMyListbox.Requery

This assumes that the listbox is a single select listbox. If it is a
multiselect listbox you will need to loop through the selected items and run
the query for each of them. If the ID field is a text value, you'll have to
make a slight modification to enclose the value in quotes. If the column
with the ID field is the Bound Column and the listbox is single select, you
can skip the Column part of the statement.

If the listbox is multiselect, looping through the items changes this quite
a bit, if you're not comfortable with doing that, let me know and I'll
modify it.

Once the deletion is made, there is no recovery of the record.
 
Back
Top