Results 1 to 5 of 5
  1. #1
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36

    Delete Query - Use Another Table As Criteria

    I have two tables:



    • Temp Table
      • Contains the data (rows) that I want to delete using a delete query.

    • Excluded Customers
      • Contains a list of customers whose records I want to remove from the temp table.


    Here is the SQL (although I'm doing this in design view):

    Code:
    DELETE [tSAP Temp Table].[Branch account]
    FROM [tExcluded Customers] INNER JOIN [tSAP Temp Table] ON [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account];
    When I go to datasheet view, the query correctly selects the records that I want deleted from the tempt table. But when I run the delete query, I get the error: Specify the table containing the records you want to delete

    I want to remove all rows from the temp table where there is a join on branch account (temp table) and Sold To# (excluded customers) table.

    I'm not the best with SQL but my stab at it would be something like this:

    Code:
    DELETE [tSAP Temp Table].[Branch account]
    FROM [tSAP Temp Table] 
    Where [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account];

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Okay, you're trying to delete records, not fields, right?

    You're probably looking for one of these two, depending on which table you're trying to delete records from.
    Code:
    DELETE * 
    FROM [tExcluded Customers] 
    WHERE [tExcluded Customers].[SOLD TO#] IN 
      (SELECT [Branch account] 
       FROM [tSAP Temp Table]);
    Code:
    DELETE * 
    FROM [tSAP Temp Table]
    WHERE [tSAP Temp Table].[Branch account] IN
      (SELECT [SOLD TO#] 
       FROM [tExcluded Customers]);
    Warning: As always, back up your real database and try this in a test version before you do it in the real database.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Edit: Use what Dal provided
    --------


    You need both tables in the FROM clause.

    I'm not the best at this. It usually takes me a couple of tries to get the query to do what I want, not what I tell it to do.

    Try this:

    Create a new query in design view.
    Add both tables. The ahould be linked by the fields
    [tExcluded Customers].[SOLD TO#] and [tSAP Temp Table].[Branch account]

    In the first column of the grid, add the "*" from table [tSAP Temp Table]
    In the 2nd column of the grid, add the field [Branch account].

    In SQL view, your query should look like:
    Code:
    SELECT [tSAP Temp Table].*, [tSAP Temp Table].[Branch account]
    FROM [tExcluded Customers] INNER JOIN [tSAP Temp Table] ON [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account];
    Change back to Design view.
    Now add a criteria to the 2nd column:[tExcluded Customers].[SOLD TO#]

    In SQL view, you should have:
    Code:
    SELECT [tSAP Temp Table].*, [tSAP Temp Table].[Branch account]
    FROM [tExcluded Customers] INNER JOIN [tSAP Temp Table] ON [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account]
    Where [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account];
    Change back to Design view. Change the query to a DELETE query.
    In the 3rd row (Delete) of the first column, you should see "FROM"
    In the 3rd row (Delete) of the second column, you should see "WHERE"

    In SQL view, you should have:
    Code:
    DELETE *
    FROM [tSAP Temp Table] INNER JOIN [tSAP Temp Table] ON [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account]
    Where [tExcluded Customers].[SOLD TO#] = [tSAP Temp Table].[Branch account];

    Again the warning..... Try this on a COPY of the dB. I still struggle with creating delete queries using joined tables. I don't create them very often.

    This query will (should) delete the entire row of data, not just the data from one field.

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    This thread http://stackoverflow.com/questions/5...en-using-joins suggests
    Code:
    Delete Table1.*
    From Table1
    Where Exists( Select 1 From Table2 Where Table2.MatchName = Table1.MatchName ) = True
    which seems roughly like an IN test. So, I'd expect my code to work.

    It also suggests that when using the JOIN format, using DISTINCTROW helps Access from getting confused about whether the join might return duplicate copies of the item to be deleted.

  5. #5
    Madmartigan is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2013
    Posts
    36
    Quote Originally Posted by Dal Jeanis View Post
    Okay, you're trying to delete records, not fields, right?
    Correct.

    Quote Originally Posted by Dal Jeanis View Post
    Code:
     DELETE * 
     FROM [tSAP Temp Table]
     WHERE [tSAP Temp Table].[Branch account] IN
       (SELECT [SOLD TO#] 
        FROM [tExcluded Customers]);
    This code looks like it did the trick!! Thank you for the replies!!

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

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2012, 06:19 PM
  2. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  3. make table query wants to delete itself
    By peter_lawton in forum Queries
    Replies: 11
    Last Post: 12-15-2011, 12:28 PM
  4. Replies: 11
    Last Post: 03-30-2011, 01:08 PM
  5. Replies: 0
    Last Post: 04-08-2010, 12:22 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