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.
– gato
By query you mean a query in sql or what? So it became very widespread..
– Guilherme Raguzzoni
@Denercarvalho could exemplify please?
– Guilherme Raguzzoni
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?
– gato
I added the code, what should happen is it is activated only once a day, and not every time the main form is accessed.
– Guilherme Raguzzoni
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
– Thiago Friedman
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?
– Guilherme Raguzzoni