Results 1 to 7 of 7
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    Delete a record from a table

    I have a form with a list box [lstAttachedFiles]. I want to delete the record from the list box. I looked up a code: Docmd.Execute "DELETE * FROM tblTrainingAttachment WHERE ScheduleID = Me.lstAttachedFiles"
    It took me to the debugger where it just highlighted the .Execute. Is there a way to make a button to do this?

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try:
    Docmd.RunSQL "DELETE * FROM tblTrainingAttachment WHERE ScheduleID = " & Me.lstAttachedFiles
    instead.

    See: https://docs.microsoft.com/en-us/off...s.docmd.runsql

    Note that your variable has to be outside of the quotes, or else it will be treated as literal text.
    This is assuming that you ID field is numeric. If it is text, you will need to add single quotes, i.e.
    Docmd.RunSQL "DELETE * FROM tblTrainingAttachment WHERE ScheduleID = '" & Me.lstAttachedFiles & "'"

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    That worked perfectly. Thanks. Is there a way to delete the file from the server also? I have 2 text boxes to show where the file on the server is: [txtFolderPath] and [txtFileName]. Not only does the record need to be deleted from the database, but the file needs to be deleted from the server. Is there a way to do that also?

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Use Kill [txtFolderPath] & "" & [txtFileName]

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/kill-statement
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Perfect. Thanks a lot.

  6. #6
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I just thought of something. If the user changes his mind when Access asks if he wants to delete the file. The way I have it set up, it will delete it anyway. I just added the KILL code just after the DELETE code. How would I not kill the file on the server if he clicks on NO?

  7. #7
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    After a little thought, seems that I turn off the warnings and make my own Yes/No box and put the command there. Amazing what a cup of coffee with help with.

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

Similar Threads

  1. Replies: 10
    Last Post: 12-29-2019, 01:32 AM
  2. Replies: 1
    Last Post: 08-04-2016, 01:41 PM
  3. Delete FIRST record from a table.
    By anupludra in forum Access
    Replies: 4
    Last Post: 03-17-2015, 06:44 PM
  4. Replies: 1
    Last Post: 10-10-2014, 05:58 AM
  5. delete table record with VBA
    By msasan1367 in forum Access
    Replies: 6
    Last Post: 07-07-2013, 11:29 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