Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Relational Databases
Expertise: Beginner
Apr 5, 1999

Set a Counter Field

Question:
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

Answer:
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.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap