Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with 'Chage Password' code 1

Status
Not open for further replies.

ttellis

Technical User
Aug 31, 2004
26
0
0
US
I am fairly new to Access and even moreso to coding. This database is the first one I've ever had to creat for work purposes. I am working on secuing the DB and got some help with a form for Users to change their passwords. My little helper is no longer available![thumbsdown]

Can someone please look at this code and help me to see what's wrong? My offie is running Microsoft Office XP.

Code:
'==================================================================='
'                                                                   '
'                      Change Password Form                         '
'                                                                   '
'==================================================================='
' Open this form as a dialog whenever the user needs to change his/her password.
' Members of the Admins group are allows to change any user's password.

Option Compare Database
Option Explicit

Dim strUserName As String

Private Sub cboUserName_AfterUpdate()
    strUserName = cboUserName
End Sub

Private Sub cmdCancel_Click()
    DoCmd.Close
End Sub

Private Sub cmdOK_Click()
    Dim strOldPW As String, strNewPW As String, strVerifyPW As String
    
    strOldPW = Nz(txtOldPw)
    strNewPW = Nz(txtNewPW)
    strVerifyPW = Nz(txtVerify)
    If strNewPW <> strVerifyPW Then
        txtNewPW.SetFocus
        Beep
        MsgBox "The New Password does not match the Verify password" _
            & vbCrLf & vbCrLf & "Verify the password by retyping it in the " _
            & "Verify box and clicking OK", vbExclamation
        Exit Sub
    End If
    On Error GoTo ErrorHandler
    DBEngine(0).Users(strUserName).NewPassword strOldPW, strNewPW
    MsgBox "The password has been changed", vbInformation
    DoCmd.Close
ErrorExit:
    Exit Sub
ErrorHandler:
    If Err.Number = 3033 Then
        Beep
        MsgBox "The password you entered into the Old Password box is incorrect." _
            & vbCrLf & vbCrLf _
            & "Please enter the correct password for this account.", vbInformation
        txtOldPw.SetFocus
        Resume ErrorExit
    Else
        Beep
        MsgBox "Run time error (" & Err.Number & "):" & vbCrLf & vbCrLf _
            & Err.Description, vbExclamation
        DoCmd.Close
    End If
End Sub

Private Sub Form_Open(Cancel As Integer)
    strUserName = CurrentUser()
    If UserIsInGroup(strUserName, "Admins") Then
        cboUserName.RowSource = UserNameList()
        cboUserName = strUserName
        cboUserName.Visible = True
        txtUserName.Visible = False
    Else
        txtUserName = strUserName
        txtUserName.Visible = True
        cboUserName.Visible = False
    End If
End Sub

Private Function UserIsInGroup(UserName As String, GroupName As String) As Boolean
    Dim grp As ADO.Group
    
    On Error Resume Next
    Set grp = DBEngine(0).Users(UserName).Groups(GroupName)
    UserIsInGroup = Not grp Is Nothing
    Set grp = Nothing
End Function

Private Function UserNameList()
    Dim usr As DAO.User
    Dim strResult As String
    
    For Each usr In DBEngine(0).Users
        If usr.Name <> "Creator" And usr.Name <> "Engine" Then _
            strResult = strResult & usr.Name & ","
    Next usr
    UserNameList = Left$(strResult, Len(strResult) - 1)
End Function
 
This is the code from my FAQ. What kind of problem are you having?

If you tried to contact me via a FAQ comment, I didn't reply because (a) the FAQ comment feature is meant for making suggestions, not for requesting technical help, and (b) I could only have replied via email. I don't distribute my email address for any reason, and I certainly don't want to fill my inbox with requests for technical help. That's what Tek-Tips is for.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
You are exactly right!! I couldn't figure out any other way to ask questions about this. I copied the code from your FAQ a few weeks ago to give me a reference for what I want to do. I created the whole thing as your FAQ said to see what it would do & how it would work. The form wouldn't ever open because of compiling errors. Yesterday, I locked myself out of the DB while working on securing it...the last back-up copy I made was before I started on the password form. So...I was hoping someone could point out a few things I don't understand so that I can create my own. Like I said, I'm new to this.

* I looked at the code and originally thought that the form wasn't opening because the function to open the form wasn't the first part of the code, so I moved it. Does the code need to be recorded in the order in which they will be executed?
* I set out on a quest to find out the difference between "ADO" and DAO". My version of Access apparently doesn't recognize "DAO" (kept getting compile errors at the first mention of it). I switched them to see if they were equivalent depending on the version...but it was futile. Would you happen to know of a good source that I can look at to find the right statements (this one and a few others) for things I'm trying to do? Microsoft's website has been no help to me.
* Lastly, by looking at the code, I could not for certain identify if it was looking for the User Names in a table within the DB or in the workgroup file.



[smile]Tiffany[smile]
 
I believe the initial compile errors you got were because you did not have a reference to the DAO library. I should have documented that requirement in the FAQ, and will go back and add it after I post this. (In the days when I wrote that FAQ, most people were still using Access 97, in which the DAO library was present by default.)

About locking yourself out of the database: Weren't you using the User-Level Security Wizard? You should, because setting up security manually is very error prone, and it's easy to make a database look secure when in fact it has a big fat hole in it.

It's too bad you lost your copy, because I'm going to tell you to build it again from the instructions in my FAQ. Then, before you compile it, go to Tools|References on the menu, scroll down to find 'DAO 3.6 Object Library', and set the check mark by it. Click OK. The code should compile error free after that. You will also be able to examine the DAO objects using the Object Browser, and press F1 for help file topics on them, if you want to learn more about DAO.

In response to your questions:
1. It doesn't matter at all what order the procedures are in.
2. The User and Group names are being retrieved from the current workgroup file via the DBEngine object (which is in DAO).

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Awesome! It worked...now I have a great example of how mine needs to work. Thanks SOOOOO much for your help!

[smile]Tiffany[smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top