Results 1 to 10 of 10
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    Trying to delete a record from a query


    I am trying to remove a record from a query using the code below but keep getting an error on the "CurrentDb.Execute sSQL" line saying "Could not delete from specified tables".
    Code:
    Private Sub cmdLeft_Click()
    
    dim sSQL as string
    
    sSQL = "DELETE [EntityName]"
    sSQL = sSQL & vbNewLine & "FROM qdf_EntityForCustomEmail"
    sSQL = sSQL & vbNewLine & "WHERE EntityName IN ('ABC')
    
    Debug.Print sSQL
    
    CurrentDb.Execute sSQL
    
    End Sub
    Can someone please advise?

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Try using DELETE DISTINCTROW to specify unique records
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    tried that but no luck....still getting same error

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Are the data tables actually housed in Access, or are they linked tables?
    If so, where are they linked from, and are you able to actually edit those records at all from Access (if they are in Excel, you cannot; at least not that way).

    Also to clarify one point, you do not really delete data from queries. The data would be deleted from the underlying tables.
    If you do not want to delete the data from the underlying table, but rather you just want to not show it in the query, you wouldn't delete the data. You would just filter it out of the query using criteria.

  5. #5
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    That's surprising.
    Agree with all Joe's comments. If that doesn't explain it, please post the SQL for that query you are referencing.

    Also have a look at the solution in this thread https://www.access-programmers.co.uk...16#post1614816
    Explanation in post 9 but use the attachment from post 12
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  6. #6
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    Quote Originally Posted by JoeM View Post
    Are the data tables actually housed in Access, or are they linked tables?
    If so, where are they linked from, and are you able to actually edit those records at all from Access (if they are in Excel, you cannot; at least not that way).

    Also to clarify one point, you do not really delete data from queries. The data would be deleted from the underlying tables.
    If you do not want to delete the data from the underlying table, but rather you just want to not show it in the query, you wouldn't delete the data. You would just filter it out of the query using criteria.
    The query I want to delete from is made from a table > that table gets populated via importing a csv file using the method DoCmd.Transfer text.

    I don't want to remove any data from the table, I just want it to not appear in my query.
    By the way, I am using this query to set my rowsource for a list box.

  7. #7
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Try this

    Code:
    SELECT * FROM qdf_EntityForCustomerEmail WHERE EntityName <>'ABC';
    Note that you cannot run SELECT statements in VBA as there's nothing to execute.
    However that is fine for your row source
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  8. #8
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You cannot delete records from just a query without deleting them from the table.
    You simply filter them out, like I said, and isladogs just posted the code for.

  9. #9
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    cool, I just created a table and based my rowsource on that rather than a query.
    It's working now, thank you

  10. #10
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Glad its working but I see no reason to have created a new table.
    What I was suggesting was a query filtering what you already had.

    Your query qdf_EntityForCustomEmail must be based on one or more tables (or is it just a query definition?)

    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

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

Similar Threads

  1. Replies: 9
    Last Post: 11-29-2017, 10:27 AM
  2. Replies: 10
    Last Post: 05-25-2017, 02:17 PM
  3. deleted record error after delete query
    By ck4794 in forum Queries
    Replies: 3
    Last Post: 10-12-2013, 02:04 PM
  4. Replies: 7
    Last Post: 07-14-2012, 01:02 AM
  5. Trying to Delete record using delete query
    By MooseOTL in forum Access
    Replies: 13
    Last Post: 10-04-2011, 02:30 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