Ver Mensaje Individual
  #7  
Antiguo 27-09-2008
Avatar de Softweb
Softweb Softweb is offline
Miembro
 
Registrado: ago 2008
Posts: 46
Reputación: 0
Softweb Va por buen camino
Hola te paso una funcion para hacer lo que tu quieres.

Código Delphi [-]
function GenerarId(KeyField, TableName, Condicion: string): Integer;
var
  NewValue: Integer;
begin
  NewValue := -1;
  Query1.Close;
  Query1.SQL.Clear;
  if Condicion <> '' then
    Query1.SQL.Add('SELECT Max( '+KeyField+' ) AS MAXIMO FROM '+TableName+ ' WHERE '+Condicion)
  else
    Query1.SQL.Add('SELECT Max('+KeyField+') AS MAXIMO FROM '+TableName);
  Query1.Open;
  // Si no hay registros en la tabla la consulta devuelve null
  if VarIsNull( Query1.FieldValues[ 'MAXIMO' ] ) then
    NewValue := 1 // valor para el primer registro de la tabla
  else
    NewValue := Integer( Query1.FieldValues[ 'MAXIMO' ] ) + 1;
  Query1.Close;
  Result := NewValue;
end;

Para usarla es asín.

Código Delphi [-]
NuevoNumero := GenerarId('num_veh','vehiculos.db', '');

O si lo quieres con alguna condicion.

Código Delphi [-]
NuevoNumero := GenerarId('num_veh','vehiculos.db', 'Fecha > ''01/06/2008''');


Saludos
Responder Con Cita