Error message when closing the application

Asked

Viewed 163 times

2

I have this code that I use in a login screen, that when the user uses the correct password, it directs to the main screen, until then everything ok, works perfectly, but when I fecho the application it displays a message from the code.

Invalid user name or password. If you have forgotten your password, please refer to the 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;
  • You may have assigned some other event to procedure imgLoginClick, I’ve seen it happen while duplicating buttons on the screen, check it out.

  • @David has nothing in the form onclose,

  • Everything indicates that the process is being called at some other time. In the Onexit event of some Edit, for example.

  • It makes no sense because it enters the condition that the query has no record.

  • It does. It makes perfect sense. Select is based on edtName.Text and edtPass.Text. If the eEvent onExit of this field or any other event calls the method the query is mounted without user and password and of course, it will not find any record that meets these conditions.

  • Okay @Reginaldorigo, but I guarantee it’s not being used anywhere.

  • If they are not too long, post the PAS and DFM of that screen. To post the DFM with the form on the screen press ALT+F12 and post the code.

  • dfm is quite extensive.

Show 4 more comments

2 answers

2

I made some modifications to the onClick event of the imgLogin component: (Considering the Login and Home Forms on 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 Onclose event from the Home form:

procedure TfrmHome.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Application.Terminate;
end;

0

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

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

I don’t know if it’s for you, but you could have a Mainform, and the login screen, and the rest of the program, were a Child of your Mainform.

Another situation, which has nothing to do with your question, but I think it would be interesting for you to isolate this authentication rule, because these kinds of rules are often used.

Browser other questions tagged

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