ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   insert or update (https://www.excelbanter.com/excel-programming/322141-insert-update.html)

Laurent M

insert or update
 
Hello,

i use an Access database for my Excel application i'm exporting some data
into it. I use ADO functions to insert or update data.
But i would like to know how to tell my function to insert if the primary
key doesn't exist and to update data if it exists.

Many thanks !

No Name

insert or update
 
hi,
trying to update and append and the same time is dangerous
and is not recommended. they should be run as seperate
operations.

-----Original Message-----
Hello,

i use an Access database for my Excel application i'm

exporting some data
into it. I use ADO functions to insert or update data.
But i would like to know how to tell my function to

insert if the primary
key doesn't exist and to update data if it exists.

Many thanks !
.


Jamie Collins

insert or update
 

wrote:
I use ADO functions to insert or update data.
But i would like to know how to tell my function to
insert if the primary
key doesn't exist and to update data if it exists.


Use the Connection object's Execute method, whose second parameter is
RecordsAffected. Do the UPDATE first and test the RecordsAffected; it
should be a simple matter to determine whether it was successful. If
the UPDATE failed, try the INSERT INTO next.

Alternatively, you could fetch a disconnected recordset and Filter it
using the PK column value and use RecordCount to determine whether it
exists (in a multi-user environment you may have to reconnect and
requery to ensure you have an up-to-date data). You could even make the
changes to the recordset, reconnect and let the recordset do the
UPDATEs and INSERTs by issuing an UpdateBatch.

trying to update and append and the same time is dangerous
and is not recommended.


Is it even possible? A recordset seemingly does this but really, under
the hood, it is executing either UPDATE or INSERT INTO as appropriate.
Jamie.

--



All times are GMT +1. The time now is 09:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com