Recent

Author Topic: SOLVED: Checkbox.checked condition reacts differently when used with Zeos code  (Read 1340 times)

mstibs

  • Newbie
  • Posts: 2
The code in question:

procedure TForm1.Button1Click(Sender: TObject);

begin

Form1.ZQuery1.SQL.Add('INSERT INTO logins (Username,Computername) VALUES (:Username,:Computername)');
Form1.ZQuery1.ParamByName('Username').AsString := GetEnvironmentVariable('USERNAME');
Form1.ZQuery1.ParamByName('Computername').AsString := GetEnvironmentVariable('COMPUTERNAME');
  begin
  if not (CheckBox1.Checked) then
       ShowMessage('Read first and check the checkbox!')
  else
       Form1.ZQuery1.ExecSQL;
       Form1.Close;
  end
end; 


I want to log if users read and confirm the data security instructions. The form works as expected when I comment all ZQuery lines. If the checkbox isn't checked, the message appears, when I click OK there, the program is still alive and waits for a click on the checkbox.

If the ZQuery lines are active and the checkbox is unchecked, the "Read first..." message appears and when I confirm it with OK, the program closes and the SQL isn't executed.

What did I do wrong?
TIA, Michael
« Last Edit: April 26, 2018, 01:11:05 pm by mstibs »

rvk

  • Hero Member
  • *****
  • Posts: 6163
First... use proper indentation and use the #-code tag button when posting code.
Second... You have the Close of the form outside the IF/ELSE statement. You didn't see that because you didn't proper indentation.
Last... you don't need "Form1." when accessing components of the form itself.

So:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ZQuery1.SQL.Add('INSERT INTO logins (Username,Computername) VALUES (:Username,:Computername)');
  4.   ZQuery1.ParamByName('Username').AsString := GetEnvironmentVariable('USERNAME');
  5.   ZQuery1.ParamByName('Computername').AsString := GetEnvironmentVariable('COMPUTERNAME');
  6.   begin
  7.     if not (CheckBox1.Checked) then
  8.       ShowMessage('Read first and check the checkbox!')
  9.     else
  10.       ZQuery1.ExecSQL;
  11.     Close;
  12.   end;
  13. end;

If you want to keep the form open then you need a begin/end around the ExecSQL and Close:
Code: Pascal  [Select][+][-]
  1.     if not (CheckBox1.Checked) then
  2.       ShowMessage('Read first and check the checkbox!')
  3.     else
  4.     begin
  5.       ZQuery1.ExecSQL;
  6.       Close;
  7.     end;

You see, proper indentation makes everything more clear.

mstibs

  • Newbie
  • Posts: 2
Thanks! Works =) I'll remember your words. ;D

Best, Michael

 

TinyPortal © 2005-2018