Connecting Tech Pros Worldwide Forums | Help | Site Map

recordset AddNew primary key issue

sudarp@gmail.com
Guest
 
Posts: n/a
#1: Aug 10 '06
hello,

I am wirting a function in VBA that adds a new record to an existing
database. If i try to add a record with a primary key that already
exists in the database, i get an error. I was wondering if there was
some way I could make AddNew overwrite any old entry with the same
primary key...I was hoping I wouldn't have to iterate through the
entire database each time I add an entry.

thanks


pietlinden@hotmail.com
Guest
 
Posts: n/a
#2: Aug 10 '06

re: recordset AddNew primary key issue



sud...@gmail.com wrote:
Quote:
hello,
>
I am wirting a function in VBA that adds a new record to an existing
database. If i try to add a record with a primary key that already
exists in the database, i get an error. I was wondering if there was
some way I could make AddNew overwrite any old entry with the same
primary key...I was hoping I wouldn't have to iterate through the
entire database each time I add an entry.
>
thanks
use DCount to see if it exists. if it does, use an update query,
otherwise, insert.

sudarp@gmail.com
Guest
 
Posts: n/a
#3: Aug 10 '06

re: recordset AddNew primary key issue



pietlinden@hotmail.com wrote:
Quote:
sud...@gmail.com wrote:
Quote:
hello,

I am wirting a function in VBA that adds a new record to an existing
database. If i try to add a record with a primary key that already
exists in the database, i get an error. I was wondering if there was
some way I could make AddNew overwrite any old entry with the same
primary key...I was hoping I wouldn't have to iterate through the
entire database each time I add an entry.

thanks
>
use DCount to see if it exists. if it does, use an update query,
otherwise, insert.
wouldn't that be the equiavlent of potentially iterating through the
database twice? (once for DCount, and once to set the cursor for update
query). I was hoping there might be a more efficient way to update
using access...

pietlinden@hotmail.com
Guest
 
Posts: n/a
#4: Aug 10 '06

re: recordset AddNew primary key issue


use DCount to see if it exists. if it does, use an update query,
Quote:
Quote:
otherwise, insert.
>
wouldn't that be the equiavlent of potentially iterating through the
database twice? (once for DCount, and once to set the cursor for update
query). I was hoping there might be a more efficient way to update
using access...
No, not really. If you use DCount, you're just checking for the
existence of the primary key value. If it exists, you have two
options: update the existing record or overwrite it. If you don't want
it, just pass the PK to a delete query, run the delete, then run the
insert.

sub ReplaceOldRecord(byval strPK as string)
dim cQUOTE as String = "'" '<--- single quote
currentDB.Execute("DELETE FROM myTable WHERE PrimaryKey=" &
cQUOTE & strPK & cQUOTE)
'put your insert here...
currentDB.Execute("INSERT INTO myTable(field1, field2...) VALUES
(me.cbo..., me.txt....)
end sub

Randy Harris
Guest
 
Posts: n/a
#5: Aug 10 '06

re: recordset AddNew primary key issue


* sudarp@gmail.com:
Quote:
pietlinden@hotmail.com wrote:
Quote:
>sud...@gmail.com wrote:
Quote:
>>hello,
>>>
>>I am wirting a function in VBA that adds a new record to an existing
>>database. If i try to add a record with a primary key that already
>>exists in the database, i get an error. I was wondering if there was
>>some way I could make AddNew overwrite any old entry with the same
>>primary key...I was hoping I wouldn't have to iterate through the
>>entire database each time I add an entry.
>>>
>>thanks
>use DCount to see if it exists. if it does, use an update query,
>otherwise, insert.
>
wouldn't that be the equiavlent of potentially iterating through the
database twice? (once for DCount, and once to set the cursor for update
query). I was hoping there might be a more efficient way to update
using access...
>
Definitely not. There would be no iterating through the database.
That's one of the advantages of indexes. DCount on a primary key is
extremely fast and efficient.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
sudarp@gmail.com
Guest
 
Posts: n/a
#6: Aug 11 '06

re: recordset AddNew primary key issue



Randy Harris wrote:
Quote:
* sudarp@gmail.com:
Quote:
pietlinden@hotmail.com wrote:
Quote:
sud...@gmail.com wrote:
>hello,
>>
>I am wirting a function in VBA that adds a new record to an existing
>database. If i try to add a record with a primary key that already
>exists in the database, i get an error. I was wondering if there was
>some way I could make AddNew overwrite any old entry with the same
>primary key...I was hoping I wouldn't have to iterate through the
>entire database each time I add an entry.
>>
>thanks
use DCount to see if it exists. if it does, use an update query,
otherwise, insert.
wouldn't that be the equiavlent of potentially iterating through the
database twice? (once for DCount, and once to set the cursor for update
query). I was hoping there might be a more efficient way to update
using access...
>
Definitely not. There would be no iterating through the database.
That's one of the advantages of indexes. DCount on a primary key is
extremely fast and efficient.
>
--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
ok, thanks!

Closed Thread