A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

delete records from one table if not in another



 
 
Thread Tools Display Modes
  #1  
Old December 18th, 2008, 04:27 PM posted to microsoft.public.access.gettingstarted
mcnewsxp[_2_]
external usenet poster
 
Posts: 7
Default delete records from one table if not in another

how can i delete records from one table if the records do not exist in a
matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able to
delete only the new records. this is not working, but you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp


  #2  
Old December 18th, 2008, 04:57 PM posted to microsoft.public.access.gettingstarted
Steve[_57_]
external usenet poster
 
Posts: 598
Default delete records from one table if not in another

Try creating an unmatched query and changing it to a delete query. There's a
wixard for creating an unmatched query by going to queries in the database
window and clicking new.

Steve


"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in a
matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able to
delete only the new records. this is not working, but you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp



  #3  
Old December 18th, 2008, 05:09 PM posted to microsoft.public.access.gettingstarted
mcnewsxp[_2_]
external usenet poster
 
Posts: 7
Default delete records from one table if not in another


"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query. There's
a wixard for creating an unmatched query by going to queries in the
database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in a
matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able to
delete only the new records. this is not working, but you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp





  #4  
Old December 18th, 2008, 06:14 PM posted to microsoft.public.access.gettingstarted
John Spencer
external usenet poster
 
Posts: 7,815
Default delete records from one table if not in another

Well, if you have imported then into tblPatient then they are in the table.
So an unmatched query is not going to show you anything.

STEP 1: BACKUP your data before attempting the following.
STEP 2: BACKUP your data before attempting the following.

Without a backup you cannot restore the data if this does not work the way you
expect. Here are four options - choose your favorite

Option 1:
DELETE
FROM TblPatient
WHERE tblPatient.PatientID in
(SELECT PatientID FROM ImportSourceTable)

Option 2: Optionally if you want to use the backup table instead of the import
table.
DELETE
FROM tblPatient
WHERE tblPatient.PatientID IN
(SELECT Temp.PatientID
FROM tblPatient as TEMP LEFT JOIN tblPatient_BU
ON Temp.PatientID = tblPatient_BU.PatientID
WHERE TblPatient_BU.PatientID is NULL)

Option 3:
On the other hand, you might just be able to replace TblPatient with
TblPatient_BU. Unless other changes have been made to the records you want to
retain, this would seem to be the easiest solution.

Option 4:
Another option would be to add a date field (DateCreated) to tblPatient and
set its default to NOW(). Then all you have to do is identify records that
were created at a certain date and time and delete them.

DELETE
FROM tblPatient
WHERE DateCreated Between #2008-12-18 13:59:00# and #2008-12-18 14:00:00#

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

mcnewsxp wrote:
"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query. There's
a wixard for creating an unmatched query by going to queries in the
database window and clicking new.


good idea.
except when i do this no fields show up in the list....???


"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in a
matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able to
delete only the new records. this is not working, but you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp




  #5  
Old December 18th, 2008, 06:18 PM posted to microsoft.public.access.gettingstarted
mcnewsxp[_2_]
external usenet poster
 
Posts: 7
Default delete records from one table if not in another


"John Spencer" wrote in message
...
Well, if you have imported then into tblPatient then they are in the
table. So an unmatched query is not going to show you anything.

STEP 1: BACKUP your data before attempting the following.
STEP 2: BACKUP your data before attempting the following.

Without a backup you cannot restore the data if this does not work the way
you expect. Here are four options - choose your favorite

Option 1:
DELETE
FROM TblPatient
WHERE tblPatient.PatientID in
(SELECT PatientID FROM ImportSourceTable)

Option 2: Optionally if you want to use the backup table instead of the
import table.
DELETE
FROM tblPatient
WHERE tblPatient.PatientID IN
(SELECT Temp.PatientID
FROM tblPatient as TEMP LEFT JOIN tblPatient_BU
ON Temp.PatientID = tblPatient_BU.PatientID
WHERE TblPatient_BU.PatientID is NULL)

Option 3:
On the other hand, you might just be able to replace TblPatient with
TblPatient_BU. Unless other changes have been made to the records you want
to retain, this would seem to be the easiest solution.

Option 4:
Another option would be to add a date field (DateCreated) to tblPatient
and set its default to NOW(). Then all you have to do is identify records
that were created at a certain date and time and delete them.

DELETE
FROM tblPatient
WHERE DateCreated Between #2008-12-18 13:59:00# and #2008-12-18 14:00:00#



the date idea is a good one.
i tried the replace but i couldn't delete the original records due to
dependancies.


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

mcnewsxp wrote:
"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries in
the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???


"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in
a matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able
to delete only the new records. this is not working, but you get the
idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp




  #6  
Old December 18th, 2008, 07:16 PM posted to microsoft.public.access.gettingstarted
Steve[_57_]
external usenet poster
 
Posts: 598
Default delete records from one table if not in another

If you make a backup of the original table and then import new records into
the backup table, the new records will not be in the original table. Your
unmatched query should look at the backup table to find records that are not
in the original table. If you do not return any records, are you specifying
an appropriate field in the unmatched query?

I am a bit puzzeled by your response "....no fields show up in the list...."
By "fields" do you mean records or do you truly mean fields? Please clarify.

Steve


"mcnewsxp" wrote in message
...

"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries in
the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in a
matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able
to delete only the new records. this is not working, but you get the
idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp







  #7  
Old December 18th, 2008, 07:17 PM posted to microsoft.public.access.gettingstarted
mcnewsxp[_2_]
external usenet poster
 
Posts: 7
Default delete records from one table if not in another


"Steve" wrote in message
m...
If you make a backup of the original table and then import new records
into the backup table, the new records will not be in the original table.
Your unmatched query should look at the backup table to find records that
are not in the original table. If you do not return any records, are you
specifying an appropriate field in the unmatched query?

I am a bit puzzeled by your response "....no fields show up in the
list...." By "fields" do you mean records or do you truly mean fields?
Please clarify.


when i go to create the unmatched query after selecting the two tables - on
the form where you select the fields to match theres nothing in the
lists...?

"mcnewsxp" wrote in message
...

"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries in
the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in
a matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able
to delete only the new records. this is not working, but you get the
idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp









  #8  
Old December 19th, 2008, 08:56 PM posted to microsoft.public.access.gettingstarted
Steve[_57_]
external usenet poster
 
Posts: 598
Default delete records from one table if not in another

Are you talking about the wizard and the two listboxes for selecting fields
to match?

Steve


"mcnewsxp" wrote in message
...

"Steve" wrote in message
m...
If you make a backup of the original table and then import new records
into the backup table, the new records will not be in the original table.
Your unmatched query should look at the backup table to find records that
are not in the original table. If you do not return any records, are you
specifying an appropriate field in the unmatched query?

I am a bit puzzeled by your response "....no fields show up in the
list...." By "fields" do you mean records or do you truly mean fields?
Please clarify.


when i go to create the unmatched query after selecting the two tables -
on the form where you select the fields to match theres nothing in the
lists...?

"mcnewsxp" wrote in message
...

"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries in
the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist in
a matching table? i am making a backup of a table before an import is
performed. the users may need to undo the backup so i need to be able
to delete only the new records. this is not working, but you get the
idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp











  #9  
Old December 21st, 2008, 03:27 PM posted to microsoft.public.access.gettingstarted
mcnewsxp[_2_]
external usenet poster
 
Posts: 7
Default delete records from one table if not in another


"Steve" wrote in message
m...
Are you talking about the wizard and the two listboxes for selecting
fields to match?


yes


"mcnewsxp" wrote in message
...

"Steve" wrote in message
m...
If you make a backup of the original table and then import new records
into the backup table, the new records will not be in the original
table. Your unmatched query should look at the backup table to find
records that are not in the original table. If you do not return any
records, are you specifying an appropriate field in the unmatched query?

I am a bit puzzeled by your response "....no fields show up in the
list...." By "fields" do you mean records or do you truly mean fields?
Please clarify.


when i go to create the unmatched query after selecting the two tables -
on the form where you select the fields to match theres nothing in the
lists...?

"mcnewsxp" wrote in message
...

"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries
in the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist
in a matching table? i am making a backup of a table before an
import is performed. the users may need to undo the backup so i need
to be able to delete only the new records. this is not working, but
you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp













  #10  
Old December 21st, 2008, 10:32 PM posted to microsoft.public.access.gettingstarted
Steve[_57_]
external usenet poster
 
Posts: 598
Default delete records from one table if not in another

1. When you go to New - Unmatched Query to start the wizard, do you get a
listbox with all your tables?

2. You need to select the Backup table first and then the original table.
Then do you get two listboxes where the left listbox shows the fields in the
backup table and the right list box shows the fields in the original table?

Steve


"mcnewsxp" wrote in message
...

"Steve" wrote in message
m...
Are you talking about the wizard and the two listboxes for selecting
fields to match?


yes


"mcnewsxp" wrote in message
...

"Steve" wrote in message
m...
If you make a backup of the original table and then import new records
into the backup table, the new records will not be in the original
table. Your unmatched query should look at the backup table to find
records that are not in the original table. If you do not return any
records, are you specifying an appropriate field in the unmatched
query?

I am a bit puzzeled by your response "....no fields show up in the
list...." By "fields" do you mean records or do you truly mean fields?
Please clarify.


when i go to create the unmatched query after selecting the two tables -
on the form where you select the fields to match theres nothing in the
lists...?

"mcnewsxp" wrote in message
...

"Steve" wrote in message
...
Try creating an unmatched query and changing it to a delete query.
There's a wixard for creating an unmatched query by going to queries
in the database window and clicking new.


good idea.
except when i do this no fields show up in the list....???



"mcnewsxp" wrote in message
...
how can i delete records from one table if the records do not exist
in a matching table? i am making a backup of a table before an
import is performed. the users may need to undo the backup so i
need to be able to delete only the new records. this is not working,
but you get the idea:

DELETE tblPatient.* FROM tblPatient INNER JOIN tblPatientBackup ON
tblPatient.PatientID = tblPatientBackup.PatientID
WHERE tblPatient.PatientID tblPatientBackup.PatientID;

tia,
mcnewxp















 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:50 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.