Connecting Tech Pros Worldwide Help | Site Map

recordset AddNew primary key issue

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

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, 06:55 PM
sudarp@gmail.com
Guest
 
Posts: n/a

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, 07:55 PM
pietlinden@hotmail.com
Guest
 
Posts: n/a

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, 07:55 PM
Randy Harris
Guest
 
Posts: n/a

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, 02:45 PM
sudarp@gmail.com
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
AddRecord issue - Visual Basic with Access technocraze answers 5 January 17th, 2007 05:47 PM
AutoNumber Regeneration jimfortune@compumarc.com answers 26 November 13th, 2005 06:28 AM
AutoNumber Regeneration jimfortune@compumarc.com answers 1 November 13th, 2005 06:16 AM
Recordset opens as read-only -- WHY?? Silvio Lopes de Oliveira answers 3 July 20th, 2005 04:59 AM