Link to home
Start Free TrialLog in
Avatar of Gerhardpet
GerhardpetFlag for Canada

asked on

Need help with Access query to delete records

I have 2 tables in Access and in Table1 I want to find records without matching records in Table2 and then delete them from Table1.

Below I have a query that will find records in ADDRESS table without matching records in LOCAL_ADDRESS table. Now all I need to know how I could get the same query to delete unmatched records in the ADDRESS table.

Can anyone help me with this?

SELECT ADDRESS.RECORD_TYPE, ADDRESS.CEV_NO, ADDRESS.ADDR_TYPE, ADDRESS.ID, ADDRESS.NAME
FROM ADDRESS LEFT JOIN LOCAL_ADDRESS ON (ADDRESS.ID = LOCAL_ADDRESS.ID) AND (ADDRESS.ADDR_TYPE = LOCAL_ADDRESS.ADDR_TYPE) AND (ADDRESS.RECORD_TYPE = LOCAL_ADDRESS.RECORD_TYPE) AND (ADDRESS.[CEV_NO] = LOCAL_ADDRESS.[CEV_NO])
WHERE (((LOCAL_ADDRESS.CEV_NO) Is Null));

Open in new window

Avatar of mbizup
mbizup
Flag of Kazakhstan image

Make a backup and try this:

DELETE * 
FROM ADDRESS 
WHERE ID NOT IN (SELECT ID FROM LOCAL_ADDRESS)

Open in new window

Avatar of Gerhardpet

ASKER

I think your idea will work but in my table all following fields are indexed. Meaning that the ID field could be the same value for 2 records but the same 2 records would not have the same value for CEV_NO. Can you give me an idea who I could make it work with all my indexed fields?

Indexed fields:
RECORD_TYPE
CEV_NO
ADDR_TYPE
ID

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works perfect! Now I want the same query to work in Navicat but it does not.

I know that I did not mention this in the original question so I will post another question and award points to you.

Thank you very much for your help and perhaps you can help me out with my other questions too.