Changing the password in the table from a form

SBBmaster09

Registered User.
Local time
Today, 15:01
Joined
Apr 26, 2013
Messages
92
Hi I am having a problem with my codes.. :banghead:

I have a login button in which if the username has an empty password or the user is his/her first time to login, another form will appear which is the Set New Password. How can I change the password of that current user, my Set New Password Form has two fields which is 'txtSetPassword1' and 'txtSetPassword2' and an OK button.

The user must enter a new password and a verification password, if the 2 fields matched, the new password entered will change the password in the table. The table name is 'tbl_UsernamesQry'.

I have done it but the main problem is, it changes the other's users password not the current user.

Here is my code for the OK button of that form Set New Password:

Private Sub btnSetOk_Click()
If Me.txtSetPassword1.Value = Me.txtSetPassword2.Value Then
Me.txtSetPassword1.SetFocus
Set db = CurrentDb
strSQL = "SELECT strUsername " & _
"FROM tbl_UsernamesQry " & _
"WHERE [Enterprise ID]=strUsername & "

Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

With rst
.Edit
rst("Password") = txtSetPassword1.Value
.Update
.Close
End With

db.Close
DoCmd.Close acForm, "frmSetPassword", acSaveNo

Else
MsgBox "Password did not match. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtSetPassword1.SetFocus
End If

End Sub

Thanks for anyone who can help.
 
SBBMaster09, not sure if I follow, but will the code not be the same for the current user as well?? It is going to update the password based on the user ID.. What seems to be the problem??

PS: Please could you use the CODE tags when posting code, makes it easier to read..
 
Thanks for suggesting to use CODE tags, i dont know about it.. i am very new here.. but thanks with that.

In the Login button there is dropdown menu for usernames, if the selection reads that user has an empty password, the Set new password form is displayed.

Login Form
Code:
If IsNull(DLookup("Password", "tbl_UsernamesQry", "[Enterprise ID]='" & strUsername & "'")) Or Me.txtPassword = "" Then
        strUsername = Me.txtUsername.Value
        DoCmd.OpenForm "frmSetPassword", acNormal, "", "", acNormal
    End If

And in the Form Set New Password is my main problem, it changes the password of another name in my table not the one currently logging in.. :mad:

Posting the Code again for better view:

Code:
Private Sub btnSetOk_Click()
       If Me.txtSetPassword1.Value = Me.txtSetPassword2.Value Then
        Me.txtSetPassword1.SetFocus
        Set db = CurrentDb
        strSQL = "SELECT [Enterprise ID] " & _
                    "FROM tbl_UsernamesQry " & _
                    "WHERE [Enterprise ID]=strUsername & "
 
                Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
 
    With rst
        .Edit
         rst("Password") = txtSetPassword1.Value
        .Update
        .Close
    End With
 
 
        db.Close
        DoCmd.Close acForm, "frmSetPassword", acSaveNo
 
      Else
        MsgBox "Password did not match. Please Try Again", vbOKOnly, _
            "Invalid Entry!"
       Me.txtSetPassword1.SetFocus
    End If
 
 
End Sub
 
Okay could you please answer the following..

* What is the Data type of [Enterprise ID] ?
* What is the Rowsource, Bound Column, Column Count, Column widths and Name of the ComboBox on the Login Form..
* Is the set password form bound to the table?

Some suggestions..

* DLookUp will return Null if the Criteria is not met.. So technically the IF will Fail you there, You might need to check if there is a Record first.. Then look for Password..
* When opening set Form, you need to either pass the Enterprice ID or Refer the Login form control to update the appropriate entry..
 
Okay could you please answer the following..

* What is the Data type of [Enterprise ID] ?
* What is the Rowsource, Bound Column, Column Count, Column widths and Name of the ComboBox on the Login Form..
* Is the set password form bound to the table?

Some suggestions..

* DLookUp will return Null if the Criteria is not met.. So technically the IF will Fail you there, You might need to check if there is a Record first.. Then look for Password..
* When opening set Form, you need to either pass the Enterprice ID or Refer the Login form control to update the appropriate entry..

Answer:
*Data type of [Enterprise ID]
> in my table tbl_UsernamesQry is Text
* Rowsource > SELECT tbl_UsernamesQry.[Enterprise ID] FROM tbl_UsernamesQry;
Bound Column > 1
Column Count > 1
Column widths> [empty]
Name> txtUsername

*Is the set password form bound to the table? Yes, its Record Source is the tbl_UsernamesQry

I have 4 sample datas on my table. 3 of them has passwords and only 1 has a null password since I am doing that if that user logins, the form will determine that it has not yet set his password, the user will put his/her new password..

In the 2nd suggestion, I think that is the problem, when the user prompts the Set New Password Form, it doesnt locate the Enterprise ID from the Login Form in which I cant still locate it how to focus the change password on that Enterprise ID.

:mad:
 
Okay, that makes things more clear.. Since the SetPassword form is bound it will be easier.. You need to locate that record, by using the WHERE condition.. So this is what you need to do..

* In the LogMeIn button, you need to verify if the user exists.. If yes then check for the password.. Instead of using two Domain functions let us simplify by using a DAO recordsets.. So, the code will be something along the lines of..
Code:
Private Sub LogMeInBtn_Click()
    Dim dbObj As DAO.Database, rstObj as DAO.Recordset
    Dim mySQL As String
    mySQL = "SELECT Password FROM tbl_UsernamesQry WHERE [Enterprise ID] = '" & Me.txtUsername & "'"
    Set dbObj = CurrentDB
    Set rstObj = dbObj.OpenRecordSet(mySQL)
    If rstObj.RecordCount <> 0 Then
        If Len(rstObj!Password & vbNullString) = 0 Then
            DoCmd.OpenForm "Form_setPassword", , , "[Enterprise ID] = '" & Me.txtUsername & "'"
        End If
    Else
        MsgBox "No user by the User Name : " & Me.txtUsername & " exists in the DB !", vbCritical, "Please try again !"
    End If
End Sub
* You can now loose some of the code in the second form, as the form is bound..
Code:
Private Sub btnSetOk_Click()
    If (Me.txtSetPassword1 <> Me.txtSetPassword2) Or Len(Me.txtSetPassword1 & vbNullString) = 0 Or Len(Me.txtSetPassword2 & vbNullString) = 0 Then
        MsgBox "Password did not match. Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtSetPassword1.SetFocus
    End If
End Sub
See if this works..
 
Last edited:
In my Login Form, the Combo Box of users are all existing according to its table, anyway I still used your code, I tried it, yes it works for Login Form, and SetNewPassword Form is shown, but in the OK button of the form it doesn't works, If I typed the passwords in the 2 textfields the table[tbl_UsernamesQry] should also be updated.
 
That is what the code does.. Since the Form is BOUND to the table [tbl_UsernameQry] it will auto update..
 
Hmm.. what may seems the problem since I copied the code to the OKButton but nothing happens, there is no msg of password changed or password mismatched.. :(
 
but nothing happens, there is no msg of password changed
Because there is no code there to say if the password has been changed.. So try adding an Else..
Code:
Private Sub btnSetOk_Click()
    If (Me.txtSetPassword1 <> Me.txtSetPassword2) Or Len(Me.txtSetPassword1 & vbNullString) = 0 Or Len(Me.txtSetPassword2 & vbNullString) = 0 Then
        MsgBox "Password did not match. Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtSetPassword1.SetFocus
   [COLOR=Blue] Else
        MsgBox "Password has been updated, please login with your new password", vbInformation, "Success"
        DoCmd.Close acForm, Me.Name
    End If
End Sub[/COLOR]
 
It still doesnt works.. but thanks to that because it is really a big help,, I've added my codes before and now its ok.. still you help a lot.. Thank you so much.. Here is my final code for that which it really works..:)

Code:
Private Sub btnSetOk_Click()
    If (Me.txtSetPassword1 <> Me.txtSetPassword2) Or Len(Me.txtSetPassword1 & vbNullString) = 0 Or Len(Me.txtSetPassword2 & vbNullString) = 0 Then
        MsgBox "Password did not match. Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtSetPassword1.SetFocus
    Else
        Set db = CurrentDb
        Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
    
        rst.Edit
        rst("Password") = txtSetPassword1.Value
        rst.Update
        rst.Close
        db.Close
    
        MsgBox "Password has been updated, please login with your new password", vbInformation, "Success"
        DoCmd.Close acForm, Me.Name
    End If
End Sub

Super Thanks. :):):):):)
 
Well I am glad that you have it working.. :)

Just for clarification, If the Form is Bound, i.e. if the Form has its recordsouce as a Table/Query, then it is ready for edit/delete/updates.. Using the where condition we go to that particular record.. However I might have missed this.. The two controls TextBox1 and TextBox2 might have not been bound, they would have been unbound.. So, all you need is..
Code:
Private Sub btnSetOk_Click()
    If (Me.txtSetPassword1 <> Me.txtSetPassword2) Or Len(Me.txtSetPassword1 & vbNullString) = 0 Or Len(Me.txtSetPassword2 & vbNullString) = 0 Then
        MsgBox "Password did not match. Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtSetPassword1.SetFocus
    Else
       [COLOR=Red][B] Me.Password = Me.txtSetPassword1[/B][/COLOR]
        MsgBox "Password has been updated, please login with your new password", vbInformation, "Success"
        DoCmd.Close acForm, Me.Name
    End If
End Sub
It is my fault.. You do not need a RecordsetObject there.. Try the above code and see if it works..
 

Users who are viewing this thread

Back
Top Bottom