Criar um controle a cada lançamento tipo 0001/2007?

Delphi

25/10/2007

Pessoal, preciso fazer um tipo de controle assim

0001/2007
0002/2007
...
...
em 2008
0001/2008
0002/2008

Bom então fiz assim
unit UtesteAutoInc;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DateUtils, IBTable, DB, IBCustomDataSet, StdCtrls, Mask,
  DBCtrls, IBQuery, IBDatabase;

type
  TForm1 = class(TForm)
    IBDatabase1: TIBDatabase;
    IBTransaction1: TIBTransaction;
    IBQuery1: TIBQuery;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    DataSource1: TDataSource;
    Label2: TLabel;
    Button1: TButton;
    IBQuery1MAIOR: TIBStringField;
    IBDataSet1: TIBTable;
    IBDataSet1AUTOINC: TIBStringField;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function NextDoc(Num, Calendar: string): string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.NextDoc(Num, Calendar: string): string;
var
 S, D: integer;
 Ano, Mes, Dia: string;
begin
 Ano := ´/´ + IntToStr(YearOf(Date));
 if MonthOf(Date) < 10 then
    Mes := ´/0´ + IntToStr(MonthOf(Date))
 else
    Mes := ´/´ + IntToStr(MonthOf(Date));
 if DayOf(Date) < 10 then
    Dia := ´/0´ + IntToStr(DayOf(Date))
 else
    Dia := ´/´ + IntToStr(DayOf(Date));
 if Calendar = ´Ano´ then Calendar := Ano;
 if Calendar = ´Mes´ then Calendar := Mes;
 if Calendar = ´Dia´ then Calendar := Dia;
 if (Num <> ´´) and (Calendar = Copy(Num, Pos(´/´, Num), Length(Calendar))) then begin
     D := (Pos(´/´, Num) - 1);
     S := StrToInt(Copy(Num, 1, D)) + 1;
     FmtStr(Num, ´¬.´ + IntToStr(D) + ´d´, [S]);
     Result := Num + Calendar;
 end else
   Result := ´0001´ + Calendar;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 vcodG, vnum : string[5];
 vcod : string[10];
 year: string[4];
begin
 //if year < copy(IBQuery1Maior.Value,6,4) then
 //   showmessage(´O ano naum corresponde ao ano atual´)
 //else
 IBDataSet1.Append;
 IBQuery1.Active := True;
 Label2.Caption :=  NextDoc(IBQuery1Maior.Value, ´Ano´);
 IBDataSet1AutoInc.Value := NextDoc(IBQuery1Maior.Value, ´Ano´);
 IBQuery1.Active := False;
 try
   IBDataSet1.Post;
   ibTransaction1.Commit;
   IBDataSet1.Close;
   IBDataSet1.Open;
 except
 on Exc:Exception do
 begin
   ShowMessage(´Não foi possível atualizar o banco de dados ´+13+
                   ´Operação Encerrada´);
   IBTransaction1.Rollback;
   IBDataSet1.Close;
   IBDataSet1.Open;

 end;
 end;
end;

end.


Só que o ano que eu começo funciona, mais se eu mudar de ano ai não funciona e fica assim gravado na tabela
0001/2008 0002/2008 0003/2008 0004/2008 0005/2008 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007 0001/2007


Alguem pode me ajudar?


Adriano_servitec

Adriano_servitec

Curtidas 0

Respostas

Edilcimar

Edilcimar

25/10/2007

var ano, mes, dia : word;
DecodeDate(Date(), ano, mes, dia);
Coloque um contador para a primeira parte (números seqüênciais)
depois pegue o último arquivo gravado e verifique o ano
temp := strtoint(copy(ultimovalorsalvo, length(valorsalvo) -3, 4));
faça a comparacao
If ano > temp then
recomeça o contador em 1, e finalmente
NumeroParaString := IntToStr(contador) + ´/´ +IntToStr(ano)


GOSTEI 0
Adriano_servitec

Adriano_servitec

25/10/2007

Desculpe Edilcimar, mais não entendi!



Como devo fazer?


GOSTEI 0
Edilcimar

Edilcimar

25/10/2007

var ano, mes, dia : word;
pegue o último valor salvo, vamos supor 1234/2007
temp := 1234/2007
agora separe as partes
numero := strtoint(copy(temp,1,4));
decodifique a data atual
decodedate(date(), ano, mes, dia);
copare o ano atual com o ano da tua seqüência
if ano = strtoint(copy(temp,length(temp)-3,4)) then
numero := numero + 1; se for o mesmo ano incrementa o número
else
numero := 1; senão recomeça a numeração
novasequencia := inttostr(numero) + ´/´ + inttostr(ano); aqui faz nova numeração independente de que ano esteja


GOSTEI 0
POSTAR