Results 1 to 6 of 6
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Delete record button from table using listbox


    I have - this is to delete the items listed on the listbox

    the idea is the user selects from the listbox and it deletes whatever production in that list is selected.

    Never tried this before and not sure why it is not working

    Code:
    Private Sub btnRemove_Click()
    Dim rs As dao.Recordset
    Dim db As dao.Database
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM tblTheatre;")
    
    
    If Me.listShows.ListIndex = -1 Then
    MsgBox "You need to select a production first from the list of shows below in order to delete one from it."
    rs.Close
    Set rs = Nothing
    Exit Sub
    End If
    
    
    If rs.RecordCount = 0 Then
    MsgBox "No records to delete."
    rs.Close
    Set rs = Nothing
    Exit Sub
    End If
    
    
    rs.MoveFirst
    With rs
    Do Until rs.EOF
            Debug.Print rs!ID
            Debug.Print Me.listShows.Column(0)
            If rs!ID = Me.listShows.Column(0) Then
                rs.Edit
                rs.Delete
                rs.Update
            End If
            rs.MoveNext
        Loop
       
    End With
     rs.Close
    Set rs = Nothing
    
    
    Me.listShows.Requery
    MsgBox "You have removed a production from the list successfully"
    
    
    End Sub

  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,521
    For starters, you need to loop the selected items of the listbox. I would just execute SQL:

    CurrentDb.Execute "DELETE * FROM TableName WHERE ID = " & ListboxReference

    But you'll need to use the variable from looping the selected items.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by pbaldy View Post
    For starters, you need to loop the selected items of the listbox. I would just execute SQL:

    CurrentDb.Execute "DELETE * FROM TableName WHERE ID = " & ListboxReference

    But you'll need to use the variable from looping the selected items.
    I thought about doing that however I still want to know how to do it the other way....

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The recordset may work, though the repeated looping of the entire table would be very inefficient. In any case, the first thing that has to change is looping the selected items and using that variable. With the commonly used variable:

    Me.listShows.ItemData(varItem)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by pbaldy View Post
    The recordset may work, though the repeated looping of the entire table would be very inefficient. In any case, the first thing that has to change is looping the selected items and using that variable. With the commonly used variable:

    Me.listShows.ItemData(varItem)
    Thanks pbaldy

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    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. Replies: 1
    Last Post: 06-21-2013, 10:41 AM
  2. Replies: 11
    Last Post: 06-05-2013, 08:15 AM
  3. Replies: 22
    Last Post: 06-12-2012, 10:02 PM
  4. How to refresh listbox after delete a record
    By uronmapu in forum Access
    Replies: 7
    Last Post: 06-09-2012, 09:16 AM
  5. Command button help - delete record
    By Zukster in forum Forms
    Replies: 0
    Last Post: 08-27-2009, 08:47 AM

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