Connecting Tech Pros Worldwide Help | Site Map

recordset AddNew primary key issue

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 10th, 2006, 05:35 PM
sudarp@gmail.com
Guest
 
Posts: n/a
Default recordset AddNew primary key issue

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


  #2  
Old August 10th, 2006, 05:35 PM
pietlinden@hotmail.com
Guest
 
Posts: n/a
Default 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.

  #3  
Old August 10th, 2006, 05:55 PM
sudarp@gmail.com
Guest
 
Posts: n/a
Default 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...

  #4  
Old August 10th, 2006, 06:55 PM
pietlinden@hotmail.com
Guest
 
Posts: n/a
Default 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

  #5  
Old August 10th, 2006, 06:55 PM
Randy Harris
Guest
 
Posts: n/a
Default 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.
  #6  
Old August 11th, 2006, 01:45 PM
sudarp@gmail.com
Guest
 
Posts: n/a
Default 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!

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.