Delphi – Database Username and Password in Delphi

delphidelphi-7

I would like to create a type of login window in Delphi. Unfortunately I can't get it to match the username and password.

I have a basic .mdb database, with an Users table. In this table, there is a Username and Password. I want Delphi to check the username and password in the database and if it matches those in the edit boxes, it goes to the next form or shows a message, otherwise it does nothing. When I enter the database's first row, username and password values, I get success, but with the second, nothing. I feel like I need a way to get it to move on to the second rows values and check those and so on. There is also currently no relationships in the database.

This is my layout of Data Access: ADOConnection -> ADOTable -> DataSource

Thanks in advance!

Best Answer

As per your guess, one solution can be to move record by record to check each one. Example:

function MatchPass(Table: TADOTable; const Name, Pass: string): Boolean;
begin
  Result := False;
  Table.First;
  while not Table.Eof do begin
    if Table.FieldByName('Username').AsString = Name then begin
      Result := Table.FieldByName('Password').AsString = Pass;
      Exit;
    end;
    Table.Next;
  end;
end;

Can be called like:

  if MatchPass(ADOTable1, Edit1.Text, Edit2.Text) then
    ..


Another solution can be to let the ADOTable search for a corresponding record:

function MatchPass(Table: TADOTable; const Name, Pass: string): Boolean;
begin
  Result := Table.Locate('Username;Password', VarArrayOf([Name, Pass]), []);
end;
Related Topic