Results 1 to 11 of 11
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    Trying to Delete a Record Based on Selected Item in a ListBox

    Greetings...



    I am trying to create code that will allow a user to delete a record based on selecting the record in a (Single Select) ListBox

    Below is just one variation of what I am trying to get to work.
    Code:
    Dim Dbs As DAO.Database
    Set Dbs = CurrentDb
    
    CurrentDb.Execute "DELETE FROM TblTmpTktEmail WHERE TblTmpTktEmail.EmpID = '" & Me.ListEmailAgnts.ItemsSelected & "'"
    I have tried setting a variable using ItemData and also using .'Selected' & .'SelectedItems' et al

    The error I'm getting is a Compile/Arg Not Optional

    Thanks in advance for any help...

  2. #2
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Update...

    Not sure if this matters, but the query is in the ListBox's DoubleClick Event.

    Why I want to use the .ItemsSelected method is because one of the reasons a user will use this is when they have accidently added two of the same record

    In this case they would need to delete only the duplicate record - I did have code that worked but it deleted both the original and the duplicate

    Thanks again...

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If you make tblTmpTktEmail.EmpID 'Indexed, duplicates OK = NO', user won't be able to create a duplicate in the first place.

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    is EmpID a string? or a number?
    you dont need the .itemselected.
    is EmpID the firstr or 0 column in the listbox?
    Is EmpID unique?

    For listboxes I usually put a small "Delete" label under the listbox. The user can then select a record click the label and then I use a y/n msgbox to confirm the delete

  5. #5
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Not sure how to tag these threads as 'Solved' but this one is...

    Moke - Great suggestion!

    I went with the below Code in conjunction with Dave's instruction to Index the table with Dups = No - and it works lightening fast
    Code:
    Private Sub ListEmailAgnts_DblClick(Cancel As Integer)
    
    '***CLEAR INCORRECT ENTRY FROM TABLE & LISTBOX***
    
    If ListEmailAgnts = "" Then
      Exit Sub
    End If
    
    Dim Dbs As DAO.Database
    Set Dbs = CurrentDb
    
    CurrentDb.Execute "DELETE FROM TblTmpTktEmail WHERE TblTmpTktEmail.EmpID = '" & Me.ListEmailAgnts.Column(1) & "'"
    
    Me.ListEmailAgnts.RowSource = "SELECT TblTmpTktEmail.EmpName, TblTmpTktEmail.EmpID " & _
                                  "FROM TblTmpTktEmail"
    End Sub
    Thanks Dave & Moke!

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Also, you declared Dbs but didn't use it in the query. And don't forget to release it when you're done.

    Code:
    Dim Dbs As DAO.Database
    Set Dbs = CurrentDb
    
    Dbs.Execute "DELETE FROM TblTmpTktEmail WHERE TblTmpTktEmail.EmpID = '" & Me.ListEmailAgnts & "'"
    Set Dbs = Nothing
    What is the data type of tblTmpTktEmail.EmpID? That is usually a number data type. If it's a number data type then you do not need the single quotes in your sql string

    Usually in this situation your listbox would have multiple columns, the first column being the ID field, the other columns for display purposes. When your listbox has multiple columns, and one selection, then Me.Listbox will return the value in the first column of the selected row. If you want to return a value in a different column then you'd use Me.Listbox.Column( ColumnIndex ) where ColumnIndex starts at 0.

    Also note that if you're calling this code from outside the OnClick event of the control it would be a good idea to test if the listbox has anything selected at all before executing the sql: If Not IsNull(Me.Listbox) Then 'proceed

  7. #7
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    You can probably replace this
    Code:
    Me.ListEmailAgnts.RowSource = "SELECT TblTmpTktEmail.EmpName, TblTmpTktEmail.EmpID " & _
                                  "FROM TblTmpTktEmail"
    With this:
    Code:
    Me.ListEmailAgnts.Requery
    Assuming you the sql or your rowsource never actually changes.

  8. #8
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    kd - Awesome!

    In this case the EmpID's do have letters so they are formatted as text... also they appear in column 1 (the 2nd column) of the ListBox

    And yeah, I suppose it would be nice if I used my declared variable for Dbs - Geez... jsmh

    Requery - Works great!

    Thanks kd!

  9. #9
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    You're welcome!

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Before you go...

    with the .execute method I would add a dbfailonerror.

    I'm also not sure that a listbox would return an empty string, its probably a null but you can test for
    both using nz()

    Code:
    Private Sub ListEmailAgnts_DblClick(Cancel As Integer)
    
    Dim strSql as String
    
    If nz(Me.ListEmailAgnts.Column(1),"") <> "" Then
    
    strSql = "DELETE * FROM TblTmpTktEmail WHERE TblTmpTktEmail.EmpID = '" & Me.ListEmailAgnts.Column(1) & "'"
    
    
    CurrentDb.Execute strSql,dbFailOnError
    
    Me.ListEmailAgnts.Requery
    
    End If
                              
    End Sub

  11. #11
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Moke ~ Thank you!

    Your suggestions - Done!...

    Thank you, again!

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

Similar Threads

  1. Replies: 22
    Last Post: 10-14-2017, 04:23 AM
  2. Replies: 1
    Last Post: 05-21-2017, 12:31 AM
  3. Show Checkbox when listbox item is selected
    By gammaman in forum Modules
    Replies: 5
    Last Post: 06-04-2015, 09:46 AM
  4. Query Criteria Based on Item Selected???
    By P.Malius in forum Queries
    Replies: 5
    Last Post: 04-16-2013, 02:09 AM
  5. Replies: 3
    Last Post: 11-29-2011, 12:54 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