How to perform a procedure only once?

Asked

Viewed 264 times

1

I set up a payment generator that takes multiple data and saves it in a bank table, the problem is that I used the Form Onactivate event, so every time I go back to the initial form, the procedure runs again, and this data should only be entered once a day(payment for the current day). Any hint on how to run this only when the program is first started on a daily basis(taking into account that the user can close and open again)?

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 performs in the Form Onactivate, and makes the insertion of the values in the Table Monthly Payment, as for this is all ok and it inserts... But it runs every time it comes back in the initial form, which is the problem.

  • Make a query checking whether the release was made on the current date, if nothing returns you know that the release should be made if it returns means that the release has already been made.

  • By query you mean a query in sql or what? So it became very widespread..

  • @Denercarvalho could exemplify please?

  • If you are working with database, you can do a search. More suggest you add more information to the question. For example: The code that is in the Onactive event what it does?

  • I added the code, what should happen is it is activated only once a day, and not every time the main form is accessed.

  • as @Denercarvalho said, you can make a query by returning a field from the database to see if it has already been run to the database, if the field returns null/empty, you fill this field in the database and so it will run only once to the database, even if you close or open the program

  • But what if there is data from other days? Could this field be the current date? Because there may be several data from the same day.. But they will only be entered if there is none on that same day, right?

Show 2 more comments

1 answer

0


I followed the tips of @Thiagofriedman and @Denercarvalho and it worked, I created a filter for the table Mensalidade using the current date and from there I checked if there was any data already inserted, and then I saw if I should insert or not, as follows the code above, has been fixed is working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.