Error message when closing application

2

I have this code that I use on a login screen, which when the user uses the correct password, it directs to the main screen, so everything okay, it works perfectly, but when I fecho the application it displays a message of the code.

  

Invalid user name or password. If you have forgotten your password, consult your system administrator

procedure TFrmLogin.imgLoginClick(Sender: TObject);
var StrSqlLog, mensagem: String;
 begin
  sleep(500);
  strSqlLog:= 'select u.*, p.* '+
  'from usuario u inner join perfil p '+
  'on u.nivel = p.id '+
  'where u.nome = '+#39+
  edtName.Text+
  #39 + ' and u.senha = ' +
  #39 + edtPass.Text + #39;

  FrmHome.QueryLogin.Close;
  FrmHome.QueryLogin.SQL.Clear;
  FrmHome.QueryLogin.SQL.Add(strSqlLog);
  FrmHome.QueryLogin.Open();

   if FrmHome.QueryLogin.RecordCount = 1 then
  begin
    FreeAndNil(FrmLogin);
    Application.CreateForm(TFrmhome, Frmhome);
    Application.Run;
  end;

  if FrmHome.QueryLogin.RecordCount = 0 then
  begin
    mensagem:= 'Nome ou senha do usuário '+
    'inválidos.' + #13 + #13 +
    'Se você esqueceu sua '+
    'senha, consulte ' + #13 +
    'o administrador do sistema.';

    Application.MessageBox(PChar
    (mensagem),
    'Login não autorizado',
    MB_OK+MB_IconError);

    edtName.Clear;
    edtPass.Clear;
    edtName.SetFocus;
  end;
    
asked by anonymous 20.05.2016 / 16:28

2 answers

2

I made some modifications to the onClick event of the imgLogin component: (Considering the Login and Home forms of the Auto-create side in the project options).

procedure TfrmLogin.LoginClick(Sender: TObject);
var StrSqlLog, mensagem: String;
 begin
  sleep(500);

  strSqlLog:= 'select u.*, p.* '+
  'from usuario u inner join perfil p '+
  'on u.nivel = p.id '+
  'where u.nome = '+#39+
  edtName.Text+
  #39 + ' and u.senha = ' +
  #39 + edtPass.Text + #39;

  FrmHome.QueryLogin.Close;
  FrmHome.QueryLogin.SQL.Clear;
  FrmHome.QueryLogin.SQL.Add(strSqlLog);
  FrmHome.QueryLogin.Open();

  if FrmHome.QueryLogin.RecordCount = 0 then
  begin
    mensagem:= 'Nome ou senha do usuário '+
    'inválidos.' + #13 + #13 +
    'Se você esqueceu sua '+
    'senha, consulte ' + #13 +
    'o administrador do sistema.';

    Application.MessageBox(PChar
    (mensagem),
    'Login não autorizado',
    MB_OK+MB_IconError);

    edtName.Clear;
    edtPass.Clear;
    edtName.SetFocus;
  end // fim do IF
  else
  begin
    // esconde a tela
    frmlogin.Hide;
    // chama a tela principal
    frmHome.ShowModal;
    // remove a tela Login da memória
    frmlogin.Release;
    // atribui conteúdo nulo para variável de tela frmLogin
    frmlogin := nil;
  end; // fim do Else

end; // fim da procedure LoginClick

Finally includes code for the Home Form's OnClose event:

procedure TfrmHome.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Application.Terminate;
end;
    
28.07.2016 / 17:46
0

The problem is the way you are implementing it. Note that if Rec is = 1, you will create the application, this will interrupt the process.

A simple test to do is to put a breakpoint in the .Run, so you will realize that the rest of this code will only be triggered when closing the Application you created.

I do not know if it suits you, but you could have a Mainform, and the login screen, and the rest of the program, would be a Child of your Mainform.

Another situation, which does not have to do with your question, but I think it would be interesting to isolate this authentication rule, as these types of rules are often used.

    
20.06.2016 / 13:25