Results 1 to 5 of 5
  1. #1
    ankitmehtta is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2011
    Posts
    3

    Deleting multiple records selected in a Listbox


    Hi,
    I have a listbox where I choose a value and then press a command button which runs a query to delete the selected item from a table.
    Now,The query doesn't work with a multi-select listbox. I want to be able to select multiple entries in the listbox and then delete all the selected entries from a table by clicking on a command button. I understand that this cannot be done using queries and I am not good with VBA. I am using Access 2000. Please let me know If I need to post more information. Any help would be appreciated. Thank you so much.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    A multi-select listbox doesn't have a value you can use directly in a query; you have to loop it in code to get the selected values. To do what you're talking about you could modify this:

    http://www.baldyweb.com/MultiselectAppend.htm

    Within the loop instead of using the recordset to add a record, use SQL to delete one:

    CurrentDb.Execute "DELETE * FROM TableName WHERE KeyfieldName = " & ctl.ItemData(varItem)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ankitmehtta is offline Novice
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    3

    Pictures

    Uploading pictures of my listbox, table where I want to delete values from and the query I am using to delete values currently.

  4. #4
    ankitmehtta is offline Novice
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    3
    Paul,
    Thank you so much. Appreciate your help. I am posting the code that worked.


    Private Sub Command89_Click()
    Set ctl = Me.consumed_lbx
    For Each varItem In ctl.ItemsSelected
    CurrentDb.Execute "DELETE * FROM steel_onhand_tbl WHERE ID = " & ctl.ItemData(varItem)
    Next varItem
    Forms!steel_consumed.Recalc
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Happy to help, and welcome to the site!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subform requery after multiple records selected
    By AccessBlues in forum Forms
    Replies: 8
    Last Post: 08-04-2011, 11:03 AM
  2. Replies: 3
    Last Post: 02-01-2011, 09:47 AM
  3. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 PM
  4. Exporting report selected from a listbox to excel
    By GARCHDEA in forum Import/Export Data
    Replies: 1
    Last Post: 08-10-2010, 07:45 AM
  5. deleting records
    By radicalrik in forum Queries
    Replies: 2
    Last Post: 07-14-2010, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums