Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread increment a field
Mon, Apr 14 2008 12:41 PMPermanent Link

Khaled Homouda
Hello all
I have a table which is already a child table with a field called category where I need to enter either 'Right' or 'left' in it and another field called number where I need to autincrement numbers in it, in
code, accordeing to either 'left' or 'right'. I don't know how to implement it, code or SQL thanks

Khaled
Mon, Apr 14 2008 1:53 PMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Khaled


I'm not sure just what you're asking for but if my interpretation is right it should be simple enough. You need another small table (AUTOLR) to hold the last number used. I'd have two fields LeftRight (string 1 - primary ID) and LastNumber (integer). There would be two records L and R. LastNumber would hold the last number used in the fields you need to autoinc.

Create a BeforePost event for the table you're editing and in it check the value of the field you edit and enter either left or right. Use that to do a AUTOLR.FindKey get the LastNumber, increment it, set the value in your table to that AND set LastNumber in AUTOLR

Job done.

Roy Lambert [Team Elevate]
Mon, Apr 14 2008 2:21 PMPermanent Link

Khaled Homouda
Thanks Roy, very nice and simple solution, I was thinking of doing it by this function just thought of

Function DBNextIntegerIn(SessionName, DatabaseName, TableName, FieldName, Olaterality: String; CycNo : Integer):LongInt;
Var
 Query : TDBISAMQuery;
 CurrentMax   : LongInt;
 NewMax       : LongInt;
Begin
 CurrentMax   := 0;
 Query := TDBISAMQuery.Create(nil);
 Try
   Query.SessionName := SessionName ;
   Query.DatabaseName := DatabaseName;
   Query.SQL.Clear;
   Query.SQL.Add('Select ');
   Query.SQL.Add('Max('+FieldName+')');
   Query.SQL.Add('From ');
   Query.Sql.Add('"'+TableName+'"');
   Query.Sql.Add('WHERE CycleNo = '+ IntToStr(CycNo) ) ;
   Query.Sql.Add('AND ovary = '+ Olaterality) ;
   Query.Open;
   Try
     CurrentMax := Query.Fields[0].AsInteger;
   Except
   End;
   NewMax   := CurrentMax + 1;
   Result := NewMax;
 Finally
   Query.Free;
 End;
End;

of course yours is simpler

thanks

Khaled
Image