Robert
Robert

Reputation: 61

Change Password Delphi v7

Delphi v7

This code is designed to allow the user to change his password. It appears to execute correctly, but the new password is not saved in the the password data field. I must have done something wrong, but I cannot see it.

procedure TForm4.btnPswordClick(Sender: TObject);
var
  I: integer;
begin
tblLogin.First;;
for I := 0 to tblLogin.RecordCount  do
Begin
If tblLogin.FieldByName('Username').Value = Edit1.Text then
if tblLogin.FieldByName('Password').Value = Edit2.Text  then
sign2.Visible := True; //Test in this case tells the application to make Label1  
visible if the //username and password are correct
tblLogin.Next;
end;
I:= I+1;  //ends search loop of records so program can move on
If sign2.Visible = False then
begin
MessageDlg('Error Username, or Password not correct',
mtConfirmation, [mbCancel], 0);
end
else
if edit3.Text <> edit4.Text  then
begin
MessageDlg('Error New Password does not match',
mtConfirmation, [mbCancel], 0);
end
else
begin
tblLogin.Edit;
tblLogin.FieldByName('Password').Value := Edit3.Text;
tblLogin.Post;
//form1.Show;
//form4.Close;
end;

Upvotes: 1

Views: 1619

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163347

My comment about formatting your code was just me being snide, but in fact I think it really would have helped you find the error yourself. Properly indented, your first loop is this:

tblLogin.First;;
for I := 0 to tblLogin.RecordCount  do
Begin
  If tblLogin.FieldByName('Username').Value = Edit1.Text then
    if tblLogin.FieldByName('Password').Value = Edit2.Text  then
      sign2.Visible := True; //Test in this case tells the application to make Label1 visible if the
                             //username and password are correct
  tblLogin.Next;
end;

The next line of code is this:

I:= I+1;  //ends search loop of records so program can move on

The comment suggests that you expect that line to cause the loop to terminate. But that line isn't in the loop. If it were in the loop, your code never would have compiled because you're not allowed to modify the loop-control variable inside a loop. Even outside the loop, the compiler should have warned you that the current value of I is undefined. That's because the compiler makes no guarantees about the final value of a loop-control variable once the loop has terminated. Adding 1 to an undefined value is still an undefined value.

Furthermore, your loop iterates over more records than your database table contains. If the upper bound of a for loop hasn't had 1 subtracted from it and isn't the result of High or Pred, then you're probably doing something wrong.

Since your I+1 line doesn't actually terminate the loop, you end up traversing all the way to the end of the table, so when you call tblLogin.Edit, you're putting the final record into edit mode, not the record with the matching account details.

Fixing the indentation will also show that you didn't even include all the code for that function. There's a begin without a matching end somewhere.

You could avoid all this code by using a single UPDATE statement on your database:

UPDATE tblLogin SET Password = ? WHERE Username = ? AND Password = ?

Upvotes: 8

Related Questions