Set a Counter Field

Is there an easy way to increase certain fields in tables working as a counter from a third table in a master/detail form? For example:

Order Table   Item Table    Counter Table    Order ID     Order ID      Count

I don’t know if you’d necessarily call it easy, but to set a counter field, I’ve found it best to use the AfterInsert method on the Master table of the relationship, so the counter gets carried down to any detail tables. For instance, let’s say you have a Counter table called Counter with a field called CountVal, that stores your current counter. I’d do something like this:

function TMainForm.GetNewCountVal : Integer;begin  with Counter do begin    Edit;    FieldByName('CountVal').AsInteger :=     FieldByName('CountVal').AsInteger + 1;    Post;    Result := FieldByName('CountVal').AsInteger;  end;end;procedure TMainForm.Table1AfterInsert(DataSet: TDataSet);begin  with Table1 do begin    if (State <> dsEdit) then      Edit;    FieldByName('Order Id').AsInteger :=       GetNewCountVal;  end;end;

That should do the trick.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: