Avatar of mtrussell
mtrussell
 asked on

Specify the table containg the records you want to delete

This it appears to be simple but getting all the stars to align on the SQL query is proving to be a pain.  

Basically, I have in Table1 a list all the IDs I want to delete out of Table2

The key field name in both tables is called ID.  What I want to do is delete all the records from Table2 that are equal to the ID in Table 1.

I have done this in a delete query with a join with the below code but it is giving be the above error

DELETE Tbl_ClientFlightControl.ID
FROM Tbl_ClientFlightControl INNER JOIN tbl_tmp_NotAsia_Clients ON Tbl_ClientFlightControl.ID = tbl_tmp_NotAsia_Clients.ID;


How do I fix the SQL code to get the desired results?
SQLMicrosoft AccessVBA

Avatar of undefined
Last Comment
mtrussell

8/22/2022 - Mon
Gustav Brock

Try with:

DELETE *
FROM Tbl_ClientFlightControl 
WHERE ID IN (SELECT ID FROM tbl_tmp_NotAsia_Clients.ID);

Open in new window

/gustav
mtrussell

ASKER
It is returning an error code saying 'Could not find file H:\Data\tbl_tmp_NotAsia_Clients.ID'

Never seen anything like this before...  weird....
Gustav Brock

Then, I guess, your table isn't linked properly.

/gustav
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mtrussell

ASKER
Its a normal table.  It's not linked... not sure... let me play around with this some more...  It's just two normal tables in a database.  I'm not even sure why it is looking on the H drive.
Ryan Chong

try this with minor amendment:

DELETE  FROM Tbl_ClientFlightControl WHERE ID IN (SELECT ID FROM tbl_tmp_NotAsia_Clients)
Gustav Brock

Yes, it should read tbl_tmp_NotAsia_Clients, not tbl_tmp_NotAsia_Clients.ID

/gustav
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mtrussell

ASKER
OK that works but now I have another issue... The ID is an Autonumber so when I create a temporary table it is taking the first number and creating an autonumber from there and doing a sequential numbering instead of just importing the ID number from Table 1...   (Access really shouldn't be this hard on something so simple...  :(

Anyway, I get the same error but how about if I skip the temporary table all together and just build it off a query...

What I want to do is delete all the records from Table 1 which are not part of a group.

DELETE Tbl_ClientFlightControl.*
FROM Tbl_ClientFlightControl LEFT JOIN Tbl_Regions ON Tbl_ClientFlightControl.Location = Tbl_Regions.Location
WHERE (((Tbl_Regions.Region) Not Like "*Americas*"));

Is there a simple way of doing this?
Gustav Brock

Use the same syntax: ... where id in (select Id from YourQuery)

where YourQuery selects from the join you mention.

/gustav
mtrussell

ASKER
But what about all the not where Location = [x]

sorry for being so slow on all this but I've been hunting and pecking at a solution for a couple hours now.  I'm a bit in over my head on this and it looks like it should be pretty simple.

This what I have but the Not Like isn't right.  :(


DELETE  FROM Tbl_ClientFlightControl WHERE Location IN (SELECT Location FROM tbl_regions Not Like "*Americas*"));
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Gustav Brock

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mtrussell

ASKER
That worked....  thanks... one last one... I need to do the same but there is a table in the middle to get to the same result... could you point out how this one works as well?

DELETE Tbl_ClientCommunication.*
FROM (Tbl_ClientFlightControl RIGHT JOIN Tbl_ClientCommunication ON Tbl_ClientFlightControl.Primary_ID = Tbl_ClientCommunication.UCL) LEFT JOIN Tbl_Regions ON Tbl_ClientFlightControl.Location = Tbl_Regions.Location
WHERE (((Tbl_Regions.Region) Not Like "*America*"));

Really appreciate all your help on this...
Gustav Brock

Create the query that lists the records to be deleted - perhaps using the query wizard.

Then insert the resulting SQL to replace the IN (...) above.

/gustav
mtrussell

ASKER
This isn't an unmatched query.  Basically, there is a field in Table 1 which links it to Table 2 which we did above.  What I am needing is to link a common identifier to Table 3.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mtrussell

ASKER
The code I did it with is above.  I would have no idea how to reference table 3 in what you showed me or I would at least make an attempt at this.