Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why do I get 'key violation' when using table.AppendRecord? 1

Status
Not open for further replies.

josefl

Programmer
Nov 29, 2002
12
SE
I'm trying to append a few records into a table. I've tried append, appendrecord (which I would prefer to use) and append-edit-setfields-post. All attempts appends a new record and enters the data into my table, but then I get the message 'Key Violation' everytime. I can't do anything. I've tried restarting my computer if I have the table open in some invisible instance. Nothing works. What is wrong?
 
The obvious answer is that you already have a record in your table with the same key as the record you are trying to append.

I suggest that you use the debugger to stop execution at the statement which is raising the Key Violation exception, make a note of the key of the record you are trying to append and then using Database Explorer check if it already exists in the table.

Andreew
 
Sorry, I think I was a bit unclear. The attempts to append adds the first record only and not all the records.
 
So, I can't have two 'Name' fields with the same name even though the other data is different?
 
If 'Name' is your primary key then it must be unique.

The second time you try to append a record with the same Name as one already in the table you will get a Key Violation error.

I don't know your application but 'Name' is not usually a good choice for a primary key. This is why most databases have entities such as a customer reference number to use as a primary key to ensure uniqueness.

You might find it a good idea to read a book about database design. Anybody care to recommend one?

Andrew



 
Some database books...

Connolly, Begg: Database Systems - A Practical Approach toDesign, Implementation and Management. Addison-Wesley, 3edition, copyright2002
An introduction to database system by C.J Date, copyright 2000
 
Thx, all. As I suspected, the problem was simple and I was stupid. The 'Name' field must be unique. Thanks.
 
Alternatively, you have a composite key, made up of several fields.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top