PC Review


Reply
Thread Tools Rate Thread

Delete Orphans Query

 
 
=?Utf-8?B?UGV0ZXIgSGliYnM=?=
Guest
Posts: n/a
 
      30th Aug 2006
Hi

I have two tables, tblOrders with 2247 records and tblOrderDetails with
30573 records. The tables are NOT linked at present. Both tables have a Long
Integer field called OrderID (Unique Index in tblOrders). Using the Unmatched
Query Wizard I have identified 27081 records in tblOrderDetails that do not
have corresponding records in tblOrders (just don't ask me how). Is there any
simple way, say by using a Delete Query, to delete these orphan records
without having to write a load of VBA code. Access will not let me delete the
list that the query returns.

This is the query :-

SELECT tblOrderDetails.ID
FROM tblOrderDetails LEFT JOIN tblOrders ON tblOrderDetails.OrderID =
tblOrders.OrderID
WHERE (((tblOrders.OrderID ) Is Null));

--
Peter Hibbs
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      30th Aug 2006
DELETE FROM tblOrderDetails
WHERE tblOrderDetails.OrderID NOT IN
(SELECT DISTINCT OrderID FROM tblOrders)


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Peter Hibbs" <(E-Mail Removed)_SPAM> wrote in message
news:53EF296F-EFB3-44C4-95B0-(E-Mail Removed)...
> Hi
>
> I have two tables, tblOrders with 2247 records and tblOrderDetails with
> 30573 records. The tables are NOT linked at present. Both tables have a
> Long
> Integer field called OrderID (Unique Index in tblOrders). Using the
> Unmatched
> Query Wizard I have identified 27081 records in tblOrderDetails that do
> not
> have corresponding records in tblOrders (just don't ask me how). Is there
> any
> simple way, say by using a Delete Query, to delete these orphan records
> without having to write a load of VBA code. Access will not let me delete
> the
> list that the query returns.
>
> This is the query :-
>
> SELECT tblOrderDetails.ID
> FROM tblOrderDetails LEFT JOIN tblOrders ON tblOrderDetails.OrderID =
> tblOrders.OrderID
> WHERE (((tblOrders.OrderID ) Is Null));
>
> --
> Peter Hibbs



 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      30th Aug 2006
This subquery should identify the records in tblOrderDetails that have no
matching OrderID value in tblOrders:

DELETE FROM tblOrderDetails
WHERE NOT EXISTS
(SELECT OrderID FROM tblOrders
WHERE tblOrders.OrderID = tblOrderDetails.OrderID);

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066

Presumably you want to create the relation with RI once this is cleaned up.
Can I also suggest that you set the Required property to Yes for OrderID in
tblOrderDetails, and remove the zero from its Default Value property. The
zero is often the source of these orphans, and the Required property is
needed even with RI.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Peter Hibbs" <(E-Mail Removed)_SPAM> wrote in message
news:53EF296F-EFB3-44C4-95B0-(E-Mail Removed)...
>
> I have two tables, tblOrders with 2247 records and tblOrderDetails with
> 30573 records. The tables are NOT linked at present. Both tables have a
> Long
> Integer field called OrderID (Unique Index in tblOrders). Using the
> Unmatched
> Query Wizard I have identified 27081 records in tblOrderDetails that do
> not
> have corresponding records in tblOrders (just don't ask me how). Is there
> any
> simple way, say by using a Delete Query, to delete these orphan records
> without having to write a load of VBA code. Access will not let me delete
> the
> list that the query returns.
>
> This is the query :-
>
> SELECT tblOrderDetails.ID
> FROM tblOrderDetails LEFT JOIN tblOrders ON tblOrderDetails.OrderID =
> tblOrders.OrderID
> WHERE (((tblOrders.OrderID ) Is Null));
>
> --
> Peter Hibbs



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgSGliYnM=?=
Guest
Posts: n/a
 
      30th Aug 2006
Thanks very much Douglas and Allen for some useful information. Tables now
linked OK.
--
Peter Hibbs


"Allen Browne" wrote:

> This subquery should identify the records in tblOrderDetails that have no
> matching OrderID value in tblOrders:
>
> DELETE FROM tblOrderDetails
> WHERE NOT EXISTS
> (SELECT OrderID FROM tblOrders
> WHERE tblOrders.OrderID = tblOrderDetails.OrderID);
>
> If subqueries are new, see:
> How to Create and Use Subqueries
> at:
> http://support.microsoft.com/?id=209066
>
> Presumably you want to create the relation with RI once this is cleaned up.
> Can I also suggest that you set the Required property to Yes for OrderID in
> tblOrderDetails, and remove the zero from its Default Value property. The
> zero is often the source of these orphans, and the Required property is
> needed even with RI.
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Peter Hibbs" <(E-Mail Removed)_SPAM> wrote in message
> news:53EF296F-EFB3-44C4-95B0-(E-Mail Removed)...
> >
> > I have two tables, tblOrders with 2247 records and tblOrderDetails with
> > 30573 records. The tables are NOT linked at present. Both tables have a
> > Long
> > Integer field called OrderID (Unique Index in tblOrders). Using the
> > Unmatched
> > Query Wizard I have identified 27081 records in tblOrderDetails that do
> > not
> > have corresponding records in tblOrders (just don't ask me how). Is there
> > any
> > simple way, say by using a Delete Query, to delete these orphan records
> > without having to write a load of VBA code. Access will not let me delete
> > the
> > list that the query returns.
> >
> > This is the query :-
> >
> > SELECT tblOrderDetails.ID
> > FROM tblOrderDetails LEFT JOIN tblOrders ON tblOrderDetails.OrderID =
> > tblOrders.OrderID
> > WHERE (((tblOrders.OrderID ) Is Null));
> >
> > --
> > Peter Hibbs

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign key value to orphans =?Utf-8?B?TmljayBY?= Microsoft Access Forms 2 28th Sep 2007 12:56 PM
deleting orphans JethroUK© Microsoft Access Queries 1 26th Aug 2006 01:22 PM
delete orphans from one to many JethroUK© Microsoft Access Form Coding 5 21st May 2006 02:33 PM
delete orphans from one to many JethroUK© Microsoft Access Queries 5 21st May 2006 02:33 PM
Orphans Don Pyeatt Microsoft Frontpage 5 10th Aug 2005 01:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 AM.