NexusDB tracks a "autoinc" value in the file header. This value starts out as 0 on a newly created table. GetAutoIncValue directly returns this value. SetAutoIncValue directly sets this value.
When a record is inserted the server checks if it contains an autoinc field and if the field is still NULL. If yes it increments the autoinc value in the table header and assigned the incremented value to the autoinc field.
That means:
• | If you assign (client side) a value to the autoinc field the server side processing will not be done and the value in the file header will not be incremented. It is your responsibility to update that value using SetAutoIncValue. |
• | You have to make sure that between your call to GetAutoIncValue and later SetAutoIncValue no one else modifies the autoinc value in the header which can happen as a result of either calling SetAutoIncValue or inserting a record into the table that contains NULL in the autoinc field. |
To achieve this the code should look like this:
var
AutoIncValue: TnxWord32;
with nxTable, Database do begin
StartTransactionWith([nxTable]);
try
Append;
try
GetAutoIncValue(AutoIncValue);
Inc(AutoIncValue);
AutoIncField.AsInteger := Integer(AutoIncValue);
SetAutoIncValue(AutoIncValue);
<SetOtherFieldValues>
Post;
except
Cancel;
raise;
end;
Commit;
except
Rollback;
raise;
end;
end;