Conditional INSERT Query

  • Thread starter Thread starter Warrio
  • Start date Start date
W

Warrio

Hello!

How is it possible to create ONE and only ONE sql query that inserts values
only if this value does not exist into the table.

I can do it by code by first searching for the value, and if the result is
null, then execute the insert query:

rs = "SELECT Field1 FROM Table1 WHERE Field1 = 3"
If rs.RecordCount <=0 then
con.excute "INSERT INTO Table1 (Field1) SELECT 3"
End If

Is it possible with one query to check if the record does not exist, if not
then insert, otherwise insert nothing.

Thanks for any suggestion.
 
Hi,


add a constraint not allowing duplicated value(s), then append the record:
it won't be appended if it is already present, with the no-dup constraint
active. As far for the error you will get in this case, check if it
correspond to the duplicated value violation and forget about the error, if
it is, or warn your user that the record was already present.


Hoping it may help,
Vanderghast, Access MVP
 
You're right!
Great thanks

Michel Walsh said:
Hi,


add a constraint not allowing duplicated value(s), then append the record:
it won't be appended if it is already present, with the no-dup constraint
active. As far for the error you will get in this case, check if it
correspond to the duplicated value violation and forget about the error,
if it is, or warn your user that the record was already present.


Hoping it may help,
Vanderghast, Access MVP
 
Back
Top