procedure tbAutoInc(Table: TTable; const FieldName: string); var Q: TQuery; begin if not Table.FieldByName(FieldName).IsNull then Exit; Q := TQuery.Create(nil); try Q.DatabaseName := Table.DatabaseName; Q.SQL.Add('select max(' + FieldName + ') from ' + Table.TableName); Q.Open; try Table.FieldByName(FieldName).AsInteger := Q.Fields[0].AsInteger +1; finally Q.Close; end; finally Q.Free; end; end; { Chame esta procedure no evento BeforePost de um Table: } procedure TForm1.Table1BeforePost(DataSet: TDataSet); begin tbAutoInc(Table1, 'Codigo'); end;
O conteúdo desta página pode ajudar alguém? Compartilhe!