Set a Counter Field

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.

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist