Subject Re: Auto-Incrementing Key Field
Author Marco Menardi
--- In IBObjects@y..., "Jack Cane" <jwcane@e...> wrote:
> I have configured the key field as an auto-incementing integer. I have it
> hooked to a tdbEdit whose read-only property is set to true. When I press
> the 'Post' button of my tdbNavigator an exception is raised with message,
> "(Field name) is a required field".

As far as I remember, the error is a client-side error. If you fill the autoincrement field only at database level, you have to set that field as "not required".
The best aproach is having the trigger that assign the field value using a generator that checks if the field is null (and assigns the value only if null), and then use the IB_Query.GeneratorLinks property for having the field value automatically assigned at client level before the post (it's assigned when you enter the Insert state), or use OnBeforePost to assign the value to the field (so it's assigned only when/if posted).

the code I suggest in the trigger is like:
...
IF ((NEW.MovCon_ID IS NULL) OR (NEW.MovCon_ID < 1)) THEN
BEGIN
NEW.MovCon_ID = gen_id(MovCon_ID_GEN, 1);
END
...

then '<1' condition is there to let you assign a negative value and escape a client check for "not null" assignment to that field, in the case you are using tools like IB_SQL to perform manual manteinance of the database.

For the OnBeforePost, there is a useful property of IB_Query:
with qryMovConRighe do
begin
FieldByName('RIGA_ID').AsInt64 := Gen_ID('RIGA_ID_GEN', 1);
end;
where 'RIGA_ID' is the key field, and 'RIGA_ID_GEN' is the name of the generator used to fill it.

regards
Marco Menardi