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 » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Append if Not Exist



 
 
Thread Tools Display Modes
  #1  
Old April 20th, 2010, 11:06 PM posted to microsoft.public.access.queries
QB
external usenet poster
 
Posts: 150
Default Append if Not Exist

I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already. How would I go about this?

Thank you,

QB

  #2  
Old April 20th, 2010, 11:14 PM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default Append if Not Exist

QB wrote:

I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already.



INSERT INTO tbl_Clients ( ClientName )
SELECT DISTINCT Tmp_Imp.[Client Name]
FROM Tmp_Imp LEFT JOIN tbl_Clients
ON Tmp_Imp.[Client Name] = tbl_Clients.ClientName
WHERE tbl_Clients.ClientName Is Null

--
Marsh
MVP [MS Access]
  #3  
Old April 20th, 2010, 11:20 PM posted to microsoft.public.access.queries
Jeff Boyce
external usenet poster
 
Posts: 8,621
Default Append if Not Exist

One way to accomplish this would be to add an index to the underlying table
into which you are trying to insert. The index would be on the field that
you with not to duplicate, and would require unique values.

But I'm concerned that if you are inserting [Client Name], what is supposed
to happen when you have two "John Smith"s as clients? And it looks like
you're putting a full name in the field ... you're never going to need to
sort by lastname, then? g

Good luck!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"QB" wrote in message
...
I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already. How would I go about this?

Thank you,

QB



  #4  
Old April 21st, 2010, 12:05 AM posted to microsoft.public.access.queries
QB
external usenet poster
 
Posts: 150
Default Append if Not Exist

Marshall,

Your SQl Statement works perfectly! Thank you. Now I just need to try and
understand it so I can stop asking these types of questions.

Could you possibly explain the Join/Where clause, if it isn't too much to ask.

QB






"Marshall Barton" wrote:

QB wrote:

I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already.



INSERT INTO tbl_Clients ( ClientName )
SELECT DISTINCT Tmp_Imp.[Client Name]
FROM Tmp_Imp LEFT JOIN tbl_Clients
ON Tmp_Imp.[Client Name] = tbl_Clients.ClientName
WHERE tbl_Clients.ClientName Is Null

--
Marsh
MVP [MS Access]
.

  #5  
Old April 21st, 2010, 01:38 AM posted to microsoft.public.access.queries
QB
external usenet poster
 
Posts: 150
Default Append if Not Exist

In this instance, client's are companies so Client Name is actually a
business' name and as such duplication should never occur.

I like the idea of the unique index on the field. So if I do this and try
to insert using VBA I'm assuming it will still throw me an error which I will
need to ignore using proper error handling (Correct me if I am wrong).

Thank you for the idea,

QB





"Jeff Boyce" wrote:

One way to accomplish this would be to add an index to the underlying table
into which you are trying to insert. The index would be on the field that
you with not to duplicate, and would require unique values.

But I'm concerned that if you are inserting [Client Name], what is supposed
to happen when you have two "John Smith"s as clients? And it looks like
you're putting a full name in the field ... you're never going to need to
sort by lastname, then? g

Good luck!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"QB" wrote in message
...
I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already. How would I go about this?

Thank you,

QB



.

  #6  
Old April 21st, 2010, 04:20 PM posted to microsoft.public.access.queries
Marshall Barton
external usenet poster
 
Posts: 5,361
Default Append if Not Exist

When the tables are joined that way, you will get each
record in Tmp joined to its matching record in Clients or
nulls if there is no matching clients record. The Where
clause just selects the records in Tmp where there is no
matching record in Clients (the Where clause can check any
field that is guaranteed to be non null in Clients).
--
Marsh
MVP [MS Access]


QB wrote:
Your SQl Statement works perfectly! Thank you. Now I just need to try and
understand it so I can stop asking these types of questions.

Could you possibly explain the Join/Where clause, if it isn't too much to ask.


"Marshall Barton" wrote:
QB wrote:
I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that do
not already exist in the table already.



INSERT INTO tbl_Clients ( ClientName )
SELECT DISTINCT Tmp_Imp.[Client Name]
FROM Tmp_Imp LEFT JOIN tbl_Clients
ON Tmp_Imp.[Client Name] = tbl_Clients.ClientName
WHERE tbl_Clients.ClientName Is Null

  #7  
Old April 21st, 2010, 07:06 PM posted to microsoft.public.access.queries
Jeff Boyce
external usenet poster
 
Posts: 8,621
Default Append if Not Exist

When I use indexes, I also trap for the error and handle it. Sounds good!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"QB" wrote in message
...
In this instance, client's are companies so Client Name is actually a
business' name and as such duplication should never occur.

I like the idea of the unique index on the field. So if I do this and try
to insert using VBA I'm assuming it will still throw me an error which I
will
need to ignore using proper error handling (Correct me if I am wrong).

Thank you for the idea,

QB





"Jeff Boyce" wrote:

One way to accomplish this would be to add an index to the underlying
table
into which you are trying to insert. The index would be on the field
that
you with not to duplicate, and would require unique values.

But I'm concerned that if you are inserting [Client Name], what is
supposed
to happen when you have two "John Smith"s as clients? And it looks like
you're putting a full name in the field ... you're never going to need to
sort by lastname, then? g

Good luck!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"QB" wrote in message
...
I created the following basic Append query

INSERT INTO tbl_Clients ( ClientName )
SELECT [Tmp_Imp].[Client Name]
FROM [Tmp_Imp]
GROUP BY [Tmp_Imp].[Client Name];

But now I need to add a twist, I need to only append those Clients that
do
not already exist in the table already. How would I go about this?

Thank you,

QB



.



 




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 09:56 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.