MS SQL: when user 2 presses Add while user 1 is still on the form, user 2 gets a duplicate key error

Applies to: Clarion 6 (C6) ABC Browse and Form templates against MS SQL, as reported. C5 is mentioned for the embed location.

Problem

A payables table has a control number key and a second UNIQUE key on vendor number plus vendor invoice number. With a standard ABC browse and form, user 1 presses Add without trouble. When user 2 presses Add while user 1 is still on the form, user 2 gets a duplicate key error, because the vendor number and invoice number of the new record are both blank in each record.

Solution

  1. The error is correct. The form template adds the record to the database before the form opens, so user 1's blank record already holds the unique key value that user 2's new record tries to insert.
  2. Prime every field of the unique key with a distinct value when the template adds the record. The original poster fixed it with the PrimeFields embed of the form (the "Field Priming On Insert" button of the Save button in the form template ends up in the PrimeFields method of the WindowManager class).
  3. If Clarion does the autonumbering, one reply says to fill the vendor fields in PrimeRecord of the browse. If Clarion is not autonumbering, priming in the form is enough.
  4. For a form that repeats the add, the prime happens in InsertAction (C6, in ABWINDOW), which calls PrimeFields after PrimeAutoInc. In C5 it is a routine of TakeCompleted with the same order. What you prime depends on who does the autonumbering.

Caveats

  • Other suggestions in the thread: remove the Unique attribute from the SQL key and enforce it in code or a trigger; add the control number as a third element of the unique key; use the SQL IDENTITY attribute instead of Clarion autonumbering, so the record is added only when the user presses OK.
  • The posters did not say which of these they would prefer. Only the PrimeFields embed was reported to have worked.

Today (2026)

Likely unchanged for ABC forms, since the add-before-form behaviour is a template design. Not re-tested. See also FAQ-09 for the related Exclude Empty Keys duplicate error.

Sources: Paul S. (Mar 2004, the question), Richard B., Jim K., Geoff B., Shankar J. (Mar 2004), Paul S. (Mar 2004, "the PrimeFields embed did the trick") in softvelocity.clarion.databasedrivers.