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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Security problems upgrading from Access 97 to XP

Status
Not open for further replies.

arniec

Programmer
Jul 22, 2003
49
US
At my company we are still running Access 97. During my tenure here I have created several databases with user-level security and, using Microsoft's Knowledge Base, I created many easy to use forms for such tasks as adding new users, changing user security levels, changing user passwords, etc. (all the code I'm using is below...there's quite a bit).

It all accesses the Microsoft MDW file using DAO.

Now we are moving to Windows XP and Office XP (2002). My code will compile, it will even run, but what it will not do is add or modify any information in the security file. I have been web surfing for several hours on this problem...does anyone know where I can find tips on where to begin either modifying the below code or starting over?

Thank you!!!!!!!!!!!!!!!!
Code:
Option Compare Database
Option Explicit

Function faq_IsUserInGroup(strGroup As String, strUser As String) As Integer
   ' Returns True if user is in group, False otherwise
   ' This only works if you're a member of the Admins group.
      Dim ws As Workspace
      Dim grp As Group
      Dim StrUsername As String

      Set ws = DBEngine.Workspaces(0)
      Set grp = ws.Groups(strGroup)
      On Error Resume Next
      StrUsername = ws.Groups(strGroup).Users(strUser).Name
      faq_IsUserInGroup = (Err = 0)
End Function

Public Function CreateUser(ByVal strUser As String, ByVal _
       strPID As String, ByVal gname As String, Optional varPwd As Variant) As Integer
   '---------------------------------------------------------------
   ' Create a new user and add them to the group specified by gname
   ' and to the default Users group
   ' Returns True on success, False if user already exists
   '===============================================================
       Dim db As Database
       Dim ws As Workspace
       Dim usr As User
       Dim grpUsers As Group
       Dim strSQL As String
       ' if the password isn't supplied, make sure you
       ' pass an empty string for the password argument
       If IsMissing(varPwd) Then varPwd = ""
       Set ws = DBEngine.Workspaces(0)
       ws.Users.Refresh
       On Error Resume Next
       ' check to see if user already exists by using inline
       ' error handling to trap any errors caused by setting
       ' a reference to a possibly non-existent user
       strUser = ws.Users(strUser).Name
       If Err.number = 0 Then
           MsgBox "The user you are trying to add already exists.", _
               vbInformation, "Can't Add User"
           CreateUser = False
       Else
           ' go ahead and create the user account
           Set usr = ws.CreateUser(strUser, strPID, varPwd)
           ws.Users.Append usr
           ws.Users.Refresh
           ' now add the user to the Users group
           Set grpUsers = ws.Groups(gname)
           Set usr = grpUsers.CreateUser(strUser)
           grpUsers.Users.Append usr
           grpUsers.Users.Refresh
           ' now add the user to the chosen group
           Set grpUsers = ws.Groups("Users")
           Set usr = grpUsers.CreateUser(strUser)
           grpUsers.Users.Append usr
           grpUsers.Users.Refresh
           CreateUser = True
       End If
End Function

Public Function ChangeResetPassword(StrAction As String, StrUsername As _
    String, StrAdminLogon As String, StrAdminPass As String, Optional _
    StrNewPassword As Variant) As Boolean
    
    ChangeResetPassword = False
    Dim ws As Workspace
    On Error GoTo ChangeResetPassword:

    ' Create a new Administrative Workspace. If The StrAction passed to the
    ' function is "Change" then change the Password of the User named in
    ' StrUsername to the password saved in StrNewPassword.
    ' If the StrAction passed is "Reset", Then reset the password of
    ' the User mentioned in StrUsername. If neither "Change" or "Reset"
    ' is passed to the function in the StrAction argument, inform the
    ' user of an error and exit the procedure.

    Set ws = DBEngine.CreateWorkspace("AdminWorkspace", StrAdminLogon, _
    StrAdminPass)
    If StrAction = "change" Then

         If Not IsNull(StrNewPassword) Then
            ws.Users(StrUsername).NewPassword "", StrNewPassword
            MsgBox "Password Change Successful", vbOKOnly
            ChangeResetPassword = True
         Else
             MsgBox "When Attempting to Change A User's Password, You " & _
               "Must Include a New Password", vbOKOnly
         End If

    ElseIf StrAction = "reset" Then
        ws.Users(StrUsername).NewPassword "", ""
        MsgBox "Password Successfully Reset", vbOKOnly
    Else
        MsgBox "You must Select a StrAction of either '" & "Change'" & _
          "' or '" & "Reset'.", vbOKOnly
    End If

    ws.Close
    Set ws = Nothing
    Exit Function

ChangeResetPassword:
         MsgBox Err.Description
   End Function

Public Function ChangeUserPassword(StrUsername As String, StrOldPassword As String, StrNewPassword As String) _
 As Boolean
    Dim Response As Variant
    ChangeUserPassword = False
    
    On Error GoTo ChangeUserPassword_Err:
    DBEngine(0).Users(StrUsername).NewPassword StrOldPassword, StrNewPassword
    ChangeUserPassword = True
    MsgBox "Password Change Successful", vbInformation
    Exit Function

ChangeUserPassword_Err:
    If Err.number = 3033 Then
        Response = MsgBox("Access violation.  Your original password was incorrect " & _
        "or you do not have permissions to modify this password.", vbOKOnly, "Password Error")
        ChangeUserPassword = False
    Else
        MsgBox Err.Description
    End If
End Function

Public Sub ChangeUserGroup(StrUsername As String, newgroup As String)
    Dim Response As Variant
    Dim db As Database
    Dim ws As Workspace
    Dim usr As User
    Dim grpUsers As Group
    Dim strSQL As String
    Dim oldgroup As String
       
    If newgroup = "Admins" Then oldgroup = "Modifiers"
    If newgroup = "Modifiers" Then oldgroup = "Admins"

    Set ws = DBEngine.Workspaces(0)
    ws.Users.Refresh
    On Error Resume Next
    ' check to see if user already exists by using inline
    ' error handling to trap any errors caused by setting
    ' a reference to a possibly non-existent user
    StrUsername = ws.Users(StrUsername).Name
    If Err.number = 0 Then
        Set usr = ws.CreateUser(StrUsername, "strPID", "varPwd")
        ws.Users.Refresh
        ' now add the user to the group
        Set grpUsers = ws.Groups(newgroup)
        Set usr = grpUsers.CreateUser(StrUsername)
        grpUsers.Users.Append usr
        grpUsers.Users.Refresh
        Set grpUsers = ws.Groups(oldgroup)
        grpUsers.Users.Delete (StrUsername)
        grpUsers.Users.Refresh
        MsgBox "User's group successfully changed.", _
            vbInformation, "Change Successful"
    Else
        MsgBox "The user you are trying to modify doesn't exist.", _
            vbInformation, "Can't Modify User"
    End If
End Sub
 
It doesn't work because you are using a standard DIM statement for all your DAO objects. In Access 97 DAO was the default technology for managing data but in Access 2000 it became ActiveX Data Objects (ADODB). This is also the case with Access XP/2002 and Access 2003. I guess I'm a little surprised that this code would even compile under the newer versions of Access because there are no user, workspace, or database objects in ADODB.

For your information Microsoft is abandoning DAO for ADODB. That means you need to make the decision of whether to keep this code or convert to ADODB and ADOX which, combined, can be used to replace DAO. If you plan to maintain the code for the long run DAO will slowly become obsolete. As newer computers are installed they will no longer have the DAO 3.6 Object Libraries or the 2.5/3.5 DAO Compatability Libraries you must have as a reference in your code.

For the short run if you simply prefix each dimension statement with a DAO you should be able to limp along.

Example:
Dim ws As DAO.Workspace
Dim grp As DAO.Group

If you start getting into a habit of prefixing all MDAC objects the application will know exactly which DLL library to use no matter what Microsoft does to us. Oh, and if you don't have the latest libraries for ADODB go to the microsoft website and download the latest MDAC versions after reviewing any knowledgebase articles about it that may impact your applications.



---------------------
scking@arinc.com
---------------------
 
scking is absolutely correct.

2K and XP will be setup to use ADO in place of DAO. You have to...
- add the DAO reference (CTRL-G to bring up the Debug / Immidiate window in VBA. Select menu item Tools -> References. Scroll down to Microsoft DAO 3.6 Object library, and enable) (While you are at it, also enable Microsoft Scripting Runtime, and read up on FSO)
- explicitly state DAO references in your code when defining your variables.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top