473,654 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT INTO without Duplicates

I'm trying to insert records in a table that has a "no duplicates"
index on two columns.

The following snippet shows where I started off...

INSERT INTO Table_A (ID_A, ID_B)
VALUES (334, 2057)

However, if these two values exist already as a "key" combination then,
the MS Access
error on the index raises (no duplicates on the index).

The following snippet shows what I want to do (in theory)...

INSERT INTO Table_A (ID_A, ID_B)
VALUES (334, 2057)

WHERE ((ID_A <> 334) AND (ID_B <> 2057))

This of course, does not work.
But after research, it is suggested to try this...

INSERT INTO Table_A (ID_A, ID_B)
SELECT 334, 2057 FROM Table_A
WHERE
NOT EXISTS (SELECT ID_A, ID_B FROM Table_A
WHERE ((ID_A <> 334) AND (ID_B <> 2057)))

This works in MS Access, but only if the Table_A has records.
If this is run on the first try, where there are no records in Table_A
(say in the beginning of a project), the INSERT does not "insert"
records. (I guess because the first SELECT returns no records)

SQL Server allows this with no reference to a table.
One can just issue a SELECT statement with "hard" values and no table
reference. The result will be the "hard" values.

In SQL Server....

SELECT 334, 2057

returns

334 2057
Is there a way to do this in MS Access, or do I just need to catch the
error code in the VBA code that runs the SQL statement? (I run this SQL
on a button click event.)
Thanks In Advance,

Christopher

Jun 15 '06 #1
1 15487
If you do something like

strSQL = "INSERT INTO ..."

and then

currentdb.execu te strSQL, dbFailOnError

duplicate values will fail, but quietly. So your code will continue.

Jun 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2650
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks. I've been having some problems with database performance... Several threads are constantly attempting INSERTs of new records into a large table - that is hundreds of thousands of records -large by my account :-) The table has a VARCHAR field...
1
34608
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then the key violation is ignored and the new record *replaces* the pre-existing record. But what about INSERT IGNORE? Is this a synonym for REPLACE INTO - i.e. it does precisely the same thing?
6
61564
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the same structure as follows Keycolumns ========== Material
0
422
by: c_kubie | last post by:
I have an update string getting a list of names that are list1 but not in list2. (Sorry for the lame example) list1 ---- Bill Bill mike tom jim
8
15959
by: petebeatty | last post by:
I have created a SQL string the properly inserts a record in the table. However, the insert does not occur at the end of the table. Instead it inserts a record after the last record that I viewed. This would be OK, except it assigns a autonumber to be one greater than the last viewed record. This causes a duplicate autonumber. I know I can change the autonumber index (Primary Key) to not allow duplicates. How can I force the insert...
1
1650
by: rickn | last post by:
Being new to VB and programming, I'm not sure how to modify the following or if required have a Select statement to NOT allow any duplicates. I'm trying not to have any duplicates in the LOTNUM_72 field. The data is coming from odbc and being inserted via ole into access. IF the LOTNUM_72 field is set as Prinary Key, then no records are inserted if any duplicates are seen. I'n not sure at all how to make the coding so if a duplicate does...
2
9427
by: rn5a | last post by:
In a ASP applicatiuon, the FOrm has a textbox & a select list where the admin can select multiple options. Basically the admin has to enter the name of a new coach in the textbox & select the soccer clubs which he will be coaching; thus he can select only one soccer club for a new coach or multiple soccer clubs. This is how I am trying it: When this Form will be submitted, the new coaches name will be inserted in a MS-Access DB table...
4
6379
by: ryushinyama | last post by:
I had to do a lot of searching to get this one to work and in doing so I saw a lot of different sites where people were looking for this answer so I thought I would put it up. If you are trying to put data in to a table where you don't want any duplicate data a primary key, unique constraint and all that prevention is the best way to go. However when you try to insert data and it conflicts with an entry you get an error then you have to...
2
8737
by: pedalpete | last post by:
I seem to have a few duplicates entries in a very large database. My database has two identical tables. One is written to on a regular basis (every few seconds) and the other is used for queries, and updated from the write table on a daily basis. I'm trying to get rid of duplicate entries for the queries, and I thought the most effective way to do this would be to run the query as INSERT INTO readtable (SELECT * FROM writetable GROUP...
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.