How to execute a procedure only once?

1

I set up a payment generator that takes a lot of data and saves it to a database table, the problem is that I used the OnActivate event of the Form, so every time I go back to the initial form, the procedure is executed again. data should only be entered once a day (payment referring to the current day). Any hints on how to do this only when the program boots for the first time a day (assuming the user can close and reopen)?

procedure TForm3.FormActivate(Sender: TObject);
var
dataa,dataontem,datatrunc : TdateTime;
year,month,day :word;
mm,dd ,a,btext,datatext:string;
b,c,i:integer;
valorMens:real;
Soma:Double;



begin

    datatrunc:=Trunc(dataa);

//-------------------------------------------------------------------
// Gerar Pagamento Mensalidade
// ------------------------------------------------------------------



  i:=0;

  a:=stringgrid2.RowCount.ToString();
  c:=strtoint(a);
  edit5.Text:=inttostr(c);
  datatext:=datetimetostr(datatrunc);

  Form1.ADOQueryMens.Close;
  Form1.ADOQueryMens.Filter:=('Data='+ QuotedStr(datatext));
  Form1.ADOQueryMens.Filtered:=true;
  Form1.ADOQueryMens.open;



  if Form1.ADOQueryMens.FieldByName('Data').IsNull then
    begin


      if c>0 then
        begin
          while i<c do
            begin
              b:=strtoint(stringgrid2.Cells[0,i]);
              btext:=inttostr(b);

              adoQueryAluno.Close;
              adoQueryAluno.Filter:=('Código='+ QuotedStr(btext));
              adoQueryAluno.Filtered:=true;
              adoQueryAluno.open;

              Soma:= 0;
              ADOQueryAluno.First;

              while not ADOQueryAluno.Eof do
                begin
                  Soma:= Soma + AdoqueryAlunoValor.Value;
                  ADOQueryAluno.Next;
                end;
                valorMens:=Soma;
              Form1.AdoTableMensalidade.Open;
              Form1.AdoTableMensalidade.Insert;

              Form1.AdoTableMensalidade.FieldValues['CodAluno']:=b;
              Form1.AdoTableMensalidade.FieldValues['Valor']:=valorMens;
              Form1.AdoTableMensalidade.FieldValues['Data']:=Trunc(dataa);
              Form1.AdoTableMensalidade.FieldValues['Pago']:=false;

              Form1.ADOTableMensalidade.Post;
              Form1.ADOTableMensalidade.Close;
              Form1.ADOTableMensalidade.Open;

              i:=i+1;

            end;
        end;
       Form1.ADOQueryMens.Filtered:=false;

    end;
end;

This executes on the Form's OnActivate, and it inserts the values into the Monthly Table, so everything is ok and it inserts right ... But it executes every time it returns to the initial form, which is the problem.

    
asked by anonymous 16.02.2016 / 00:37

1 answer

0

I followed the tips of @ThiagoFriedman and @DenerCarvalho and it worked, I created a filter for the Monthly table using the current date and from there I checked if I had any data already entered, and then whether or not it should be inserted, as the code above, has been fixed is working.

    
16.02.2016 / 14:46