PC Review


Reply
Thread Tools Rate Thread

code to delete a user/ code to move users into and out of groups

 
 
=?Utf-8?B?dHc=?=
Guest
Posts: n/a
 
      24th May 2005
Although there is a lot of useful code in the security faq sheet at
http://support.microsoft.com/default...ent/secfaq.asp
alot of it I am currently using, I am in need of a way to delete a user
programatically and also to manipulate the groups that users are members of.
Is there code somewhere to do this. I didn't see it in the security faqs.

Thanks for all help
 
Reply With Quote
 
 
 
 
Graham R Seach
Guest
Posts: n/a
 
      26th May 2005
'Enumerate all users and groups
Public Sub EnumUsersAndGroups()
Dim wrk As DAO.Workspace
Dim grp As DAO.Group
Dim usr As DAO.User

Set wrk = DBEngine(0)

'Enumerate the groups
Debug.Print "Groups."
For Each grp In wrk.Groups
Debug.Print vbTab & grp.Name
Next grp

'Enumerate the users
Debug.Print "Users."
For Each usr In wrk.Users
Debug.Print vbTab & usr.Name
Next usr

Set grp = Nothing
Set wrk = Nothing
End Sub

'-------
'Enumerate the users belonging to a specific group
Public Sub EnumGroupUsers(strGroup As String)
Dim wrk As DAO.Workspace
Dim varUser As Variant

Set wrk = DBEngine(0)

Debug.Print "Users belonging to the '" & strGroup & "' group..."
For Each varUser In wrk.Groups(strGroup).Users
Debug.Print vbTab & varUser.Name
Next varUser

Set wrk = Nothing
End Sub

'-------
'Enumerate the groups a specific user belongs to
Public Sub EnumUserGroups(strUser As String)
Dim wrk As DAO.Workspace
Dim varGroup As Variant

Set wrk = DBEngine(0)

Debug.Print "Groups to which user '" & strUser & "' belongs..."
For Each varGroup In wrk.Users(strUser).Groups
Debug.Print vbTab & varGroup.Name
Next varGroup

Set wrk = Nothing
End Sub

'-------
'Create a group
Public Sub CreateUserGroup(strGroupName As String, _
strPID As String)
Dim wrk As DAO.Workspace
Dim grp As DAO.Group

Set wrk = DBEngine(0)
On Error GoTo CreateUserGroupErr

'Create the new group
Set grp = wrk.CreateGroup(strGroupName, strPID)
ws.Groups.Append grp

CreateUserGroupErr:
Set grp = Nothing
Set wrk = Nothing
End Sub

'-------
'Delete a group
Public Sub DeleteGroup(strGroup As String)
On Error Resume Next
DBEngine(0).Groups.Delete strGroup
End Sub

'-------
'Create a user
Public Function CreateUserAccount(strUserName As String, _
strPID As String, strPassword As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)
On Error GoTo CreateUserAccountErr

'Create the new user
Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreateUserAccountErr:
Set usr = Nothing
Set wrk = Nothing
End Function

'-------
'Delete a user
Public Sub DeleteUser(strUser As String)
On Error Resume Next
DBEngine(0).Users.Delete strUser
End Sub

'-------
'Add a group to a user
Public Sub AddGroup2User(strUser As String, _
strGroup As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User
Dim grp As DAO.Group

Set wrk = DBEngine(0)
On Error Resume Next

'Create object references
Set usr = wrk.Users(strUser)
Set grp = usr.CreateGroup(strGroup)

'Add the group to the user's Groups collection
usr.Groups.Append grp
usr.Groups.Refresh

Set usr = Nothing
Set grp = Nothing
Set wrk = Nothing
End Sub

'-------
'Add a user to a group
Public Sub AddUser2Group(strUser As String, _
strGroup As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User
Dim grp As DAO.Group

Set wrk = DBEngine(0)
On Error Resume Next

'Create object references
Set grp = wrk.Groups(strUser)
Set usr = grp.CreateUser(strUser)

'Add the group to the user's Groups collection
grp.Users.Append usr
grp.Users.Refresh

Set usr = Nothing
Set grp = Nothing
Set wrk = Nothing
End Sub

'-------
'Remove a user from a group
Public Sub DeleteUserFromGroup(strUser As String, _
strGroup As String)
Dim wrk As DAO.Workspace

Set wrk = DBEngine(0)
On Error Resume Next

wrk.Users(strUser).Groups.Delete strGroup

Set wrk = Nothing
End Sub

'-------
'Determine if a user belongs to a specific group
Public Function IsUserInGroup (strUser As String, _
strGroup As String) As Boolean
Dim wrk As DAO.Workspace

Set wrk = DBEngine(0)
On Error Resume Next

IsUserInGroup = False

'Check in the Users --> Groups collection
IsUserInGroup = _
(wrk.Users(strUser).Groups(strGroup).Name = strGroup)

'You can also do it this way...
'Check in the Groups --> Users collection
'IsUserInGroup = _
(wrk.Groups(strGroup).Users(strUser).Name = strUser)

Set wrk = Nothing
End Function

'-------
'Change a user's password
Public Sub ChangePassword(strUser As String, _
strOldPassword As String, strNewPassword As String)
Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)
Set usr = wrk.Users(strUser)

'Change the password
usr.NewPassword strOldPassword, strNewPassword

Set usr = Nothing
Set wrk = Nothing
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

"tw" <(E-Mail Removed)> wrote in message
news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
> Although there is a lot of useful code in the security faq sheet at
> http://support.microsoft.com/default...ent/secfaq.asp
> alot of it I am currently using, I am in need of a way to delete a user
> programatically and also to manipulate the groups that users are members
> of.
> Is there code somewhere to do this. I didn't see it in the security faqs.
>
> Thanks for all help



 
Reply With Quote
 
tw
Guest
Posts: n/a
 
      26th May 2005
Thanks, I check these out.

"Graham R Seach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 'Enumerate all users and groups
> Public Sub EnumUsersAndGroups()
> Dim wrk As DAO.Workspace
> Dim grp As DAO.Group
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
>
> 'Enumerate the groups
> Debug.Print "Groups."
> For Each grp In wrk.Groups
> Debug.Print vbTab & grp.Name
> Next grp
>
> 'Enumerate the users
> Debug.Print "Users."
> For Each usr In wrk.Users
> Debug.Print vbTab & usr.Name
> Next usr
>
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Enumerate the users belonging to a specific group
> Public Sub EnumGroupUsers(strGroup As String)
> Dim wrk As DAO.Workspace
> Dim varUser As Variant
>
> Set wrk = DBEngine(0)
>
> Debug.Print "Users belonging to the '" & strGroup & "' group..."
> For Each varUser In wrk.Groups(strGroup).Users
> Debug.Print vbTab & varUser.Name
> Next varUser
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Enumerate the groups a specific user belongs to
> Public Sub EnumUserGroups(strUser As String)
> Dim wrk As DAO.Workspace
> Dim varGroup As Variant
>
> Set wrk = DBEngine(0)
>
> Debug.Print "Groups to which user '" & strUser & "' belongs..."
> For Each varGroup In wrk.Users(strUser).Groups
> Debug.Print vbTab & varGroup.Name
> Next varGroup
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Create a group
> Public Sub CreateUserGroup(strGroupName As String, _
> strPID As String)
> Dim wrk As DAO.Workspace
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error GoTo CreateUserGroupErr
>
> 'Create the new group
> Set grp = wrk.CreateGroup(strGroupName, strPID)
> ws.Groups.Append grp
>
> CreateUserGroupErr:
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Delete a group
> Public Sub DeleteGroup(strGroup As String)
> On Error Resume Next
> DBEngine(0).Groups.Delete strGroup
> End Sub
>
> '-------
> 'Create a user
> Public Function CreateUserAccount(strUserName As String, _
> strPID As String, strPassword As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
> On Error GoTo CreateUserAccountErr
>
> 'Create the new user
> Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
> wrk.Users.Append usr
>
> CreateUserAccountErr:
> Set usr = Nothing
> Set wrk = Nothing
> End Function
>
> '-------
> 'Delete a user
> Public Sub DeleteUser(strUser As String)
> On Error Resume Next
> DBEngine(0).Users.Delete strUser
> End Sub
>
> '-------
> 'Add a group to a user
> Public Sub AddGroup2User(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> 'Create object references
> Set usr = wrk.Users(strUser)
> Set grp = usr.CreateGroup(strGroup)
>
> 'Add the group to the user's Groups collection
> usr.Groups.Append grp
> usr.Groups.Refresh
>
> Set usr = Nothing
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Add a user to a group
> Public Sub AddUser2Group(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> 'Create object references
> Set grp = wrk.Groups(strUser)
> Set usr = grp.CreateUser(strUser)
>
> 'Add the group to the user's Groups collection
> grp.Users.Append usr
> grp.Users.Refresh
>
> Set usr = Nothing
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Remove a user from a group
> Public Sub DeleteUserFromGroup(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> wrk.Users(strUser).Groups.Delete strGroup
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Determine if a user belongs to a specific group
> Public Function IsUserInGroup (strUser As String, _
> strGroup As String) As Boolean
> Dim wrk As DAO.Workspace
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> IsUserInGroup = False
>
> 'Check in the Users --> Groups collection
> IsUserInGroup = _
> (wrk.Users(strUser).Groups(strGroup).Name = strGroup)
>
> 'You can also do it this way...
> 'Check in the Groups --> Users collection
> 'IsUserInGroup = _
> (wrk.Groups(strGroup).Users(strUser).Name = strUser)
>
> Set wrk = Nothing
> End Function
>
> '-------
> 'Change a user's password
> Public Sub ChangePassword(strUser As String, _
> strOldPassword As String, strNewPassword As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
> Set usr = wrk.Users(strUser)
>
> 'Change the password
> usr.NewPassword strOldPassword, strNewPassword
>
> Set usr = Nothing
> Set wrk = Nothing
> End Sub
>
> Regards,
> Graham R Seach
> Microsoft Access MVP
> Sydney, Australia
> ---------------------------
>
> "tw" <(E-Mail Removed)> wrote in message
> news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
>> Although there is a lot of useful code in the security faq sheet at
>> http://support.microsoft.com/default...ent/secfaq.asp
>> alot of it I am currently using, I am in need of a way to delete a user
>> programatically and also to manipulate the groups that users are members
>> of.
>> Is there code somewhere to do this. I didn't see it in the security
>> faqs.
>>
>> Thanks for all help

>
>



 
Reply With Quote
 
tw
Guest
Posts: n/a
 
      26th May 2005
I have added the code below to delete a user, and it seems to be working,
however to see the results, you need to exit the project and return. When
you return the user is no longer there.

"Graham R Seach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 'Enumerate all users and groups
> Public Sub EnumUsersAndGroups()
> Dim wrk As DAO.Workspace
> Dim grp As DAO.Group
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
>
> 'Enumerate the groups
> Debug.Print "Groups."
> For Each grp In wrk.Groups
> Debug.Print vbTab & grp.Name
> Next grp
>
> 'Enumerate the users
> Debug.Print "Users."
> For Each usr In wrk.Users
> Debug.Print vbTab & usr.Name
> Next usr
>
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Enumerate the users belonging to a specific group
> Public Sub EnumGroupUsers(strGroup As String)
> Dim wrk As DAO.Workspace
> Dim varUser As Variant
>
> Set wrk = DBEngine(0)
>
> Debug.Print "Users belonging to the '" & strGroup & "' group..."
> For Each varUser In wrk.Groups(strGroup).Users
> Debug.Print vbTab & varUser.Name
> Next varUser
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Enumerate the groups a specific user belongs to
> Public Sub EnumUserGroups(strUser As String)
> Dim wrk As DAO.Workspace
> Dim varGroup As Variant
>
> Set wrk = DBEngine(0)
>
> Debug.Print "Groups to which user '" & strUser & "' belongs..."
> For Each varGroup In wrk.Users(strUser).Groups
> Debug.Print vbTab & varGroup.Name
> Next varGroup
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Create a group
> Public Sub CreateUserGroup(strGroupName As String, _
> strPID As String)
> Dim wrk As DAO.Workspace
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error GoTo CreateUserGroupErr
>
> 'Create the new group
> Set grp = wrk.CreateGroup(strGroupName, strPID)
> ws.Groups.Append grp
>
> CreateUserGroupErr:
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Delete a group
> Public Sub DeleteGroup(strGroup As String)
> On Error Resume Next
> DBEngine(0).Groups.Delete strGroup
> End Sub
>
> '-------
> 'Create a user
> Public Function CreateUserAccount(strUserName As String, _
> strPID As String, strPassword As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
> On Error GoTo CreateUserAccountErr
>
> 'Create the new user
> Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
> wrk.Users.Append usr
>
> CreateUserAccountErr:
> Set usr = Nothing
> Set wrk = Nothing
> End Function
>
> '-------
> 'Delete a user
> Public Sub DeleteUser(strUser As String)
> On Error Resume Next
> DBEngine(0).Users.Delete strUser
> End Sub
>
> '-------
> 'Add a group to a user
> Public Sub AddGroup2User(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> 'Create object references
> Set usr = wrk.Users(strUser)
> Set grp = usr.CreateGroup(strGroup)
>
> 'Add the group to the user's Groups collection
> usr.Groups.Append grp
> usr.Groups.Refresh
>
> Set usr = Nothing
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Add a user to a group
> Public Sub AddUser2Group(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
> Dim grp As DAO.Group
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> 'Create object references
> Set grp = wrk.Groups(strUser)
> Set usr = grp.CreateUser(strUser)
>
> 'Add the group to the user's Groups collection
> grp.Users.Append usr
> grp.Users.Refresh
>
> Set usr = Nothing
> Set grp = Nothing
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Remove a user from a group
> Public Sub DeleteUserFromGroup(strUser As String, _
> strGroup As String)
> Dim wrk As DAO.Workspace
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> wrk.Users(strUser).Groups.Delete strGroup
>
> Set wrk = Nothing
> End Sub
>
> '-------
> 'Determine if a user belongs to a specific group
> Public Function IsUserInGroup (strUser As String, _
> strGroup As String) As Boolean
> Dim wrk As DAO.Workspace
>
> Set wrk = DBEngine(0)
> On Error Resume Next
>
> IsUserInGroup = False
>
> 'Check in the Users --> Groups collection
> IsUserInGroup = _
> (wrk.Users(strUser).Groups(strGroup).Name = strGroup)
>
> 'You can also do it this way...
> 'Check in the Groups --> Users collection
> 'IsUserInGroup = _
> (wrk.Groups(strGroup).Users(strUser).Name = strUser)
>
> Set wrk = Nothing
> End Function
>
> '-------
> 'Change a user's password
> Public Sub ChangePassword(strUser As String, _
> strOldPassword As String, strNewPassword As String)
> Dim wrk As DAO.Workspace
> Dim usr As DAO.User
>
> Set wrk = DBEngine(0)
> Set usr = wrk.Users(strUser)
>
> 'Change the password
> usr.NewPassword strOldPassword, strNewPassword
>
> Set usr = Nothing
> Set wrk = Nothing
> End Sub
>
> Regards,
> Graham R Seach
> Microsoft Access MVP
> Sydney, Australia
> ---------------------------
>
> "tw" <(E-Mail Removed)> wrote in message
> news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
>> Although there is a lot of useful code in the security faq sheet at
>> http://support.microsoft.com/default...ent/secfaq.asp
>> alot of it I am currently using, I am in need of a way to delete a user
>> programatically and also to manipulate the groups that users are members
>> of.
>> Is there code somewhere to do this. I didn't see it in the security
>> faqs.
>>
>> Thanks for all help

>
>



 
Reply With Quote
 
Graham R Seach
Guest
Posts: n/a
 
      27th May 2005
Works fine for me. Try adding DBEngine(0).Users.Refresh.

'Delete a user
Public Sub DeleteUser(strUser As String)
On Error Resume Next
DBEngine(0).Users.Delete strUser
DBEngine(0).Users.Refresh
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyT...764559036.html


"tw" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have added the code below to delete a user, and it seems to be working,
>however to see the results, you need to exit the project and return. When
>you return the user is no longer there.
>
> "Graham R Seach" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 'Enumerate all users and groups
>> Public Sub EnumUsersAndGroups()
>> Dim wrk As DAO.Workspace
>> Dim grp As DAO.Group
>> Dim usr As DAO.User
>>
>> Set wrk = DBEngine(0)
>>
>> 'Enumerate the groups
>> Debug.Print "Groups."
>> For Each grp In wrk.Groups
>> Debug.Print vbTab & grp.Name
>> Next grp
>>
>> 'Enumerate the users
>> Debug.Print "Users."
>> For Each usr In wrk.Users
>> Debug.Print vbTab & usr.Name
>> Next usr
>>
>> Set grp = Nothing
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Enumerate the users belonging to a specific group
>> Public Sub EnumGroupUsers(strGroup As String)
>> Dim wrk As DAO.Workspace
>> Dim varUser As Variant
>>
>> Set wrk = DBEngine(0)
>>
>> Debug.Print "Users belonging to the '" & strGroup & "' group..."
>> For Each varUser In wrk.Groups(strGroup).Users
>> Debug.Print vbTab & varUser.Name
>> Next varUser
>>
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Enumerate the groups a specific user belongs to
>> Public Sub EnumUserGroups(strUser As String)
>> Dim wrk As DAO.Workspace
>> Dim varGroup As Variant
>>
>> Set wrk = DBEngine(0)
>>
>> Debug.Print "Groups to which user '" & strUser & "' belongs..."
>> For Each varGroup In wrk.Users(strUser).Groups
>> Debug.Print vbTab & varGroup.Name
>> Next varGroup
>>
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Create a group
>> Public Sub CreateUserGroup(strGroupName As String, _
>> strPID As String)
>> Dim wrk As DAO.Workspace
>> Dim grp As DAO.Group
>>
>> Set wrk = DBEngine(0)
>> On Error GoTo CreateUserGroupErr
>>
>> 'Create the new group
>> Set grp = wrk.CreateGroup(strGroupName, strPID)
>> ws.Groups.Append grp
>>
>> CreateUserGroupErr:
>> Set grp = Nothing
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Delete a group
>> Public Sub DeleteGroup(strGroup As String)
>> On Error Resume Next
>> DBEngine(0).Groups.Delete strGroup
>> End Sub
>>
>> '-------
>> 'Create a user
>> Public Function CreateUserAccount(strUserName As String, _
>> strPID As String, strPassword As String)
>> Dim wrk As DAO.Workspace
>> Dim usr As DAO.User
>>
>> Set wrk = DBEngine(0)
>> On Error GoTo CreateUserAccountErr
>>
>> 'Create the new user
>> Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
>> wrk.Users.Append usr
>>
>> CreateUserAccountErr:
>> Set usr = Nothing
>> Set wrk = Nothing
>> End Function
>>
>> '-------
>> 'Delete a user
>> Public Sub DeleteUser(strUser As String)
>> On Error Resume Next
>> DBEngine(0).Users.Delete strUser
>> End Sub
>>
>> '-------
>> 'Add a group to a user
>> Public Sub AddGroup2User(strUser As String, _
>> strGroup As String)
>> Dim wrk As DAO.Workspace
>> Dim usr As DAO.User
>> Dim grp As DAO.Group
>>
>> Set wrk = DBEngine(0)
>> On Error Resume Next
>>
>> 'Create object references
>> Set usr = wrk.Users(strUser)
>> Set grp = usr.CreateGroup(strGroup)
>>
>> 'Add the group to the user's Groups collection
>> usr.Groups.Append grp
>> usr.Groups.Refresh
>>
>> Set usr = Nothing
>> Set grp = Nothing
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Add a user to a group
>> Public Sub AddUser2Group(strUser As String, _
>> strGroup As String)
>> Dim wrk As DAO.Workspace
>> Dim usr As DAO.User
>> Dim grp As DAO.Group
>>
>> Set wrk = DBEngine(0)
>> On Error Resume Next
>>
>> 'Create object references
>> Set grp = wrk.Groups(strUser)
>> Set usr = grp.CreateUser(strUser)
>>
>> 'Add the group to the user's Groups collection
>> grp.Users.Append usr
>> grp.Users.Refresh
>>
>> Set usr = Nothing
>> Set grp = Nothing
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Remove a user from a group
>> Public Sub DeleteUserFromGroup(strUser As String, _
>> strGroup As String)
>> Dim wrk As DAO.Workspace
>>
>> Set wrk = DBEngine(0)
>> On Error Resume Next
>>
>> wrk.Users(strUser).Groups.Delete strGroup
>>
>> Set wrk = Nothing
>> End Sub
>>
>> '-------
>> 'Determine if a user belongs to a specific group
>> Public Function IsUserInGroup (strUser As String, _
>> strGroup As String) As Boolean
>> Dim wrk As DAO.Workspace
>>
>> Set wrk = DBEngine(0)
>> On Error Resume Next
>>
>> IsUserInGroup = False
>>
>> 'Check in the Users --> Groups collection
>> IsUserInGroup = _
>> (wrk.Users(strUser).Groups(strGroup).Name = strGroup)
>>
>> 'You can also do it this way...
>> 'Check in the Groups --> Users collection
>> 'IsUserInGroup = _
>> (wrk.Groups(strGroup).Users(strUser).Name = strUser)
>>
>> Set wrk = Nothing
>> End Function
>>
>> '-------
>> 'Change a user's password
>> Public Sub ChangePassword(strUser As String, _
>> strOldPassword As String, strNewPassword As String)
>> Dim wrk As DAO.Workspace
>> Dim usr As DAO.User
>>
>> Set wrk = DBEngine(0)
>> Set usr = wrk.Users(strUser)
>>
>> 'Change the password
>> usr.NewPassword strOldPassword, strNewPassword
>>
>> Set usr = Nothing
>> Set wrk = Nothing
>> End Sub
>>
>> Regards,
>> Graham R Seach
>> Microsoft Access MVP
>> Sydney, Australia
>> ---------------------------
>>
>> "tw" <(E-Mail Removed)> wrote in message
>> news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
>>> Although there is a lot of useful code in the security faq sheet at
>>> http://support.microsoft.com/default...ent/secfaq.asp
>>> alot of it I am currently using, I am in need of a way to delete a user
>>> programatically and also to manipulate the groups that users are members
>>> of.
>>> Is there code somewhere to do this. I didn't see it in the security
>>> faqs.
>>>
>>> Thanks for all help

>>
>>

>
>



 
Reply With Quote
 
TC
Guest
Posts: n/a
 
      27th May 2005


tw wrote:

(snip)

> >> I am in need of a way to delete a user
> >> programatically and also to manipulate
> >> the groups that users are members of.



Be careful! In at least one version of Access/Jet, if you changed the
/current user's/ groups at runtime, the changes were visible in the
relevant collections, but they did not actually /take effect/ until you
logged-out, then logged back in.

So, if you plan to change the /current user's/ groups at runtime, do
some simple testing first, to be sure that it will work the way you
expect.

HTH,
TC

 
Reply With Quote
 
tw
Guest
Posts: n/a
 
      27th May 2005
It's there but not working. I see TC's post until after I posted mine, but
it's working like TC said it does. Is there a way to enable the system menu
for the admin users, but not for anyone else?? I know there should be a way
for me to have a custom menu but I havn't figured it out yet so I have
disable the menu on start up and am using a switchboard. but I'd like to
also have a menu and include the security features for the admin user.

"Graham R Seach" <gseach@NOSPAM_pacificdb.com.au> wrote in message
news:(E-Mail Removed)...
> Works fine for me. Try adding DBEngine(0).Users.Refresh.
>
> 'Delete a user
> Public Sub DeleteUser(strUser As String)
> On Error Resume Next
> DBEngine(0).Users.Delete strUser
> DBEngine(0).Users.Refresh
> End Sub
>
> Regards,
> Graham R Seach
> Microsoft Access MVP
> Sydney, Australia
>
> Microsoft Access 2003 VBA Programmer's Reference
> http://www.wiley.com/WileyCDA/WileyT...764559036.html
>
>
> "tw" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have added the code below to delete a user, and it seems to be working,
>>however to see the results, you need to exit the project and return. When
>>you return the user is no longer there.
>>
>> "Graham R Seach" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 'Enumerate all users and groups
>>> Public Sub EnumUsersAndGroups()
>>> Dim wrk As DAO.Workspace
>>> Dim grp As DAO.Group
>>> Dim usr As DAO.User
>>>
>>> Set wrk = DBEngine(0)
>>>
>>> 'Enumerate the groups
>>> Debug.Print "Groups."
>>> For Each grp In wrk.Groups
>>> Debug.Print vbTab & grp.Name
>>> Next grp
>>>
>>> 'Enumerate the users
>>> Debug.Print "Users."
>>> For Each usr In wrk.Users
>>> Debug.Print vbTab & usr.Name
>>> Next usr
>>>
>>> Set grp = Nothing
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Enumerate the users belonging to a specific group
>>> Public Sub EnumGroupUsers(strGroup As String)
>>> Dim wrk As DAO.Workspace
>>> Dim varUser As Variant
>>>
>>> Set wrk = DBEngine(0)
>>>
>>> Debug.Print "Users belonging to the '" & strGroup & "' group..."
>>> For Each varUser In wrk.Groups(strGroup).Users
>>> Debug.Print vbTab & varUser.Name
>>> Next varUser
>>>
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Enumerate the groups a specific user belongs to
>>> Public Sub EnumUserGroups(strUser As String)
>>> Dim wrk As DAO.Workspace
>>> Dim varGroup As Variant
>>>
>>> Set wrk = DBEngine(0)
>>>
>>> Debug.Print "Groups to which user '" & strUser & "' belongs..."
>>> For Each varGroup In wrk.Users(strUser).Groups
>>> Debug.Print vbTab & varGroup.Name
>>> Next varGroup
>>>
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Create a group
>>> Public Sub CreateUserGroup(strGroupName As String, _
>>> strPID As String)
>>> Dim wrk As DAO.Workspace
>>> Dim grp As DAO.Group
>>>
>>> Set wrk = DBEngine(0)
>>> On Error GoTo CreateUserGroupErr
>>>
>>> 'Create the new group
>>> Set grp = wrk.CreateGroup(strGroupName, strPID)
>>> ws.Groups.Append grp
>>>
>>> CreateUserGroupErr:
>>> Set grp = Nothing
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Delete a group
>>> Public Sub DeleteGroup(strGroup As String)
>>> On Error Resume Next
>>> DBEngine(0).Groups.Delete strGroup
>>> End Sub
>>>
>>> '-------
>>> 'Create a user
>>> Public Function CreateUserAccount(strUserName As String, _
>>> strPID As String, strPassword As String)
>>> Dim wrk As DAO.Workspace
>>> Dim usr As DAO.User
>>>
>>> Set wrk = DBEngine(0)
>>> On Error GoTo CreateUserAccountErr
>>>
>>> 'Create the new user
>>> Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
>>> wrk.Users.Append usr
>>>
>>> CreateUserAccountErr:
>>> Set usr = Nothing
>>> Set wrk = Nothing
>>> End Function
>>>
>>> '-------
>>> 'Delete a user
>>> Public Sub DeleteUser(strUser As String)
>>> On Error Resume Next
>>> DBEngine(0).Users.Delete strUser
>>> End Sub
>>>
>>> '-------
>>> 'Add a group to a user
>>> Public Sub AddGroup2User(strUser As String, _
>>> strGroup As String)
>>> Dim wrk As DAO.Workspace
>>> Dim usr As DAO.User
>>> Dim grp As DAO.Group
>>>
>>> Set wrk = DBEngine(0)
>>> On Error Resume Next
>>>
>>> 'Create object references
>>> Set usr = wrk.Users(strUser)
>>> Set grp = usr.CreateGroup(strGroup)
>>>
>>> 'Add the group to the user's Groups collection
>>> usr.Groups.Append grp
>>> usr.Groups.Refresh
>>>
>>> Set usr = Nothing
>>> Set grp = Nothing
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Add a user to a group
>>> Public Sub AddUser2Group(strUser As String, _
>>> strGroup As String)
>>> Dim wrk As DAO.Workspace
>>> Dim usr As DAO.User
>>> Dim grp As DAO.Group
>>>
>>> Set wrk = DBEngine(0)
>>> On Error Resume Next
>>>
>>> 'Create object references
>>> Set grp = wrk.Groups(strUser)
>>> Set usr = grp.CreateUser(strUser)
>>>
>>> 'Add the group to the user's Groups collection
>>> grp.Users.Append usr
>>> grp.Users.Refresh
>>>
>>> Set usr = Nothing
>>> Set grp = Nothing
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Remove a user from a group
>>> Public Sub DeleteUserFromGroup(strUser As String, _
>>> strGroup As String)
>>> Dim wrk As DAO.Workspace
>>>
>>> Set wrk = DBEngine(0)
>>> On Error Resume Next
>>>
>>> wrk.Users(strUser).Groups.Delete strGroup
>>>
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> '-------
>>> 'Determine if a user belongs to a specific group
>>> Public Function IsUserInGroup (strUser As String, _
>>> strGroup As String) As Boolean
>>> Dim wrk As DAO.Workspace
>>>
>>> Set wrk = DBEngine(0)
>>> On Error Resume Next
>>>
>>> IsUserInGroup = False
>>>
>>> 'Check in the Users --> Groups collection
>>> IsUserInGroup = _
>>> (wrk.Users(strUser).Groups(strGroup).Name = strGroup)
>>>
>>> 'You can also do it this way...
>>> 'Check in the Groups --> Users collection
>>> 'IsUserInGroup = _
>>> (wrk.Groups(strGroup).Users(strUser).Name = strUser)
>>>
>>> Set wrk = Nothing
>>> End Function
>>>
>>> '-------
>>> 'Change a user's password
>>> Public Sub ChangePassword(strUser As String, _
>>> strOldPassword As String, strNewPassword As String)
>>> Dim wrk As DAO.Workspace
>>> Dim usr As DAO.User
>>>
>>> Set wrk = DBEngine(0)
>>> Set usr = wrk.Users(strUser)
>>>
>>> 'Change the password
>>> usr.NewPassword strOldPassword, strNewPassword
>>>
>>> Set usr = Nothing
>>> Set wrk = Nothing
>>> End Sub
>>>
>>> Regards,
>>> Graham R Seach
>>> Microsoft Access MVP
>>> Sydney, Australia
>>> ---------------------------
>>>
>>> "tw" <(E-Mail Removed)> wrote in message
>>> news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
>>>> Although there is a lot of useful code in the security faq sheet at
>>>> http://support.microsoft.com/default...ent/secfaq.asp
>>>> alot of it I am currently using, I am in need of a way to delete a user
>>>> programatically and also to manipulate the groups that users are
>>>> members of.
>>>> Is there code somewhere to do this. I didn't see it in the security
>>>> faqs.
>>>>
>>>> Thanks for all help
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Graham R Seach
Guest
Posts: n/a
 
      30th May 2005
That behaviour is probably more to do with refreshing the document
collections.

You might try:
(a) refreshing the Containers collection:
CurrentDb.Containers.Refresh

(b) refreshing the Documents collection
CurrentDb.Containers("Tables").Documents.Refresh

(c) explicitly setting access permissions to each object for the user:
Public Sub RemoveUserTablePermissions( _
strUserName As String, strTableName As String)
Dim db As DAO.Database
Dim doc As DAO.Document

Set db = CurrentDb
Set doc = db.Containers("Tables").Documents(strTableName)

doc.UserName = strSomeUser
doc.Permissions = dbSecNoAccess

Set doc = Nothing
Set db = Nothing
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyT...764559036.html


"tw" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> It's there but not working. I see TC's post until after I posted mine,
> but it's working like TC said it does. Is there a way to enable the
> system menu for the admin users, but not for anyone else?? I know there
> should be a way for me to have a custom menu but I havn't figured it out
> yet so I have disable the menu on start up and am using a switchboard.
> but I'd like to also have a menu and include the security features for the
> admin user.
>
> "Graham R Seach" <gseach@NOSPAM_pacificdb.com.au> wrote in message
> news:(E-Mail Removed)...
>> Works fine for me. Try adding DBEngine(0).Users.Refresh.
>>
>> 'Delete a user
>> Public Sub DeleteUser(strUser As String)
>> On Error Resume Next
>> DBEngine(0).Users.Delete strUser
>> DBEngine(0).Users.Refresh
>> End Sub
>>
>> Regards,
>> Graham R Seach
>> Microsoft Access MVP
>> Sydney, Australia
>>
>> Microsoft Access 2003 VBA Programmer's Reference
>> http://www.wiley.com/WileyCDA/WileyT...764559036.html
>>
>>
>> "tw" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I have added the code below to delete a user, and it seems to be working,
>>>however to see the results, you need to exit the project and return.
>>>When you return the user is no longer there.
>>>
>>> "Graham R Seach" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 'Enumerate all users and groups
>>>> Public Sub EnumUsersAndGroups()
>>>> Dim wrk As DAO.Workspace
>>>> Dim grp As DAO.Group
>>>> Dim usr As DAO.User
>>>>
>>>> Set wrk = DBEngine(0)
>>>>
>>>> 'Enumerate the groups
>>>> Debug.Print "Groups."
>>>> For Each grp In wrk.Groups
>>>> Debug.Print vbTab & grp.Name
>>>> Next grp
>>>>
>>>> 'Enumerate the users
>>>> Debug.Print "Users."
>>>> For Each usr In wrk.Users
>>>> Debug.Print vbTab & usr.Name
>>>> Next usr
>>>>
>>>> Set grp = Nothing
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Enumerate the users belonging to a specific group
>>>> Public Sub EnumGroupUsers(strGroup As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim varUser As Variant
>>>>
>>>> Set wrk = DBEngine(0)
>>>>
>>>> Debug.Print "Users belonging to the '" & strGroup & "' group..."
>>>> For Each varUser In wrk.Groups(strGroup).Users
>>>> Debug.Print vbTab & varUser.Name
>>>> Next varUser
>>>>
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Enumerate the groups a specific user belongs to
>>>> Public Sub EnumUserGroups(strUser As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim varGroup As Variant
>>>>
>>>> Set wrk = DBEngine(0)
>>>>
>>>> Debug.Print "Groups to which user '" & strUser & "' belongs..."
>>>> For Each varGroup In wrk.Users(strUser).Groups
>>>> Debug.Print vbTab & varGroup.Name
>>>> Next varGroup
>>>>
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Create a group
>>>> Public Sub CreateUserGroup(strGroupName As String, _
>>>> strPID As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim grp As DAO.Group
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error GoTo CreateUserGroupErr
>>>>
>>>> 'Create the new group
>>>> Set grp = wrk.CreateGroup(strGroupName, strPID)
>>>> ws.Groups.Append grp
>>>>
>>>> CreateUserGroupErr:
>>>> Set grp = Nothing
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Delete a group
>>>> Public Sub DeleteGroup(strGroup As String)
>>>> On Error Resume Next
>>>> DBEngine(0).Groups.Delete strGroup
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Create a user
>>>> Public Function CreateUserAccount(strUserName As String, _
>>>> strPID As String, strPassword As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim usr As DAO.User
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error GoTo CreateUserAccountErr
>>>>
>>>> 'Create the new user
>>>> Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
>>>> wrk.Users.Append usr
>>>>
>>>> CreateUserAccountErr:
>>>> Set usr = Nothing
>>>> Set wrk = Nothing
>>>> End Function
>>>>
>>>> '-------
>>>> 'Delete a user
>>>> Public Sub DeleteUser(strUser As String)
>>>> On Error Resume Next
>>>> DBEngine(0).Users.Delete strUser
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Add a group to a user
>>>> Public Sub AddGroup2User(strUser As String, _
>>>> strGroup As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim usr As DAO.User
>>>> Dim grp As DAO.Group
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error Resume Next
>>>>
>>>> 'Create object references
>>>> Set usr = wrk.Users(strUser)
>>>> Set grp = usr.CreateGroup(strGroup)
>>>>
>>>> 'Add the group to the user's Groups collection
>>>> usr.Groups.Append grp
>>>> usr.Groups.Refresh
>>>>
>>>> Set usr = Nothing
>>>> Set grp = Nothing
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Add a user to a group
>>>> Public Sub AddUser2Group(strUser As String, _
>>>> strGroup As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim usr As DAO.User
>>>> Dim grp As DAO.Group
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error Resume Next
>>>>
>>>> 'Create object references
>>>> Set grp = wrk.Groups(strUser)
>>>> Set usr = grp.CreateUser(strUser)
>>>>
>>>> 'Add the group to the user's Groups collection
>>>> grp.Users.Append usr
>>>> grp.Users.Refresh
>>>>
>>>> Set usr = Nothing
>>>> Set grp = Nothing
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Remove a user from a group
>>>> Public Sub DeleteUserFromGroup(strUser As String, _
>>>> strGroup As String)
>>>> Dim wrk As DAO.Workspace
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error Resume Next
>>>>
>>>> wrk.Users(strUser).Groups.Delete strGroup
>>>>
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> '-------
>>>> 'Determine if a user belongs to a specific group
>>>> Public Function IsUserInGroup (strUser As String, _
>>>> strGroup As String) As Boolean
>>>> Dim wrk As DAO.Workspace
>>>>
>>>> Set wrk = DBEngine(0)
>>>> On Error Resume Next
>>>>
>>>> IsUserInGroup = False
>>>>
>>>> 'Check in the Users --> Groups collection
>>>> IsUserInGroup = _
>>>> (wrk.Users(strUser).Groups(strGroup).Name = strGroup)
>>>>
>>>> 'You can also do it this way...
>>>> 'Check in the Groups --> Users collection
>>>> 'IsUserInGroup = _
>>>> (wrk.Groups(strGroup).Users(strUser).Name = strUser)
>>>>
>>>> Set wrk = Nothing
>>>> End Function
>>>>
>>>> '-------
>>>> 'Change a user's password
>>>> Public Sub ChangePassword(strUser As String, _
>>>> strOldPassword As String, strNewPassword As String)
>>>> Dim wrk As DAO.Workspace
>>>> Dim usr As DAO.User
>>>>
>>>> Set wrk = DBEngine(0)
>>>> Set usr = wrk.Users(strUser)
>>>>
>>>> 'Change the password
>>>> usr.NewPassword strOldPassword, strNewPassword
>>>>
>>>> Set usr = Nothing
>>>> Set wrk = Nothing
>>>> End Sub
>>>>
>>>> Regards,
>>>> Graham R Seach
>>>> Microsoft Access MVP
>>>> Sydney, Australia
>>>> ---------------------------
>>>>
>>>> "tw" <(E-Mail Removed)> wrote in message
>>>> news:A824E2F0-2894-4EAF-8247-(E-Mail Removed)...
>>>>> Although there is a lot of useful code in the security faq sheet at
>>>>> http://support.microsoft.com/default...ent/secfaq.asp
>>>>> alot of it I am currently using, I am in need of a way to delete a
>>>>> user
>>>>> programatically and also to manipulate the groups that users are
>>>>> members of.
>>>>> Is there code somewhere to do this. I didn't see it in the security
>>>>> faqs.
>>>>>
>>>>> Thanks for all help
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
TC
Guest
Posts: n/a
 
      30th May 2005
Um, currentdb() refreshes all collections automatically. currentdb.<any
collection>.refresh would be superfluous, IMO.

Here's an earlier thread to do with a user's permissions /not/ changing
until he logged out & back in again:

group: this
subject: Group permissions set at startup?
date: July 2003

HTH,
TC

 
Reply With Quote
 
Graham R Seach
Guest
Posts: n/a
 
      30th May 2005
TC,

<<...currentdb() refreshes all collections automatically...>>
Yes, that's true, but it would certainly be superfluous to refresh 100
collections in order to "simplify" refreshing one.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyT...764559036.html


"TC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Um, currentdb() refreshes all collections automatically. currentdb.<any
> collection>.refresh would be superfluous, IMO.
>
> Here's an earlier thread to do with a user's permissions /not/ changing
> until he logged out & back in again:
>
> group: this
> subject: Group permissions set at startup?
> date: July 2003
>
> HTH,
> TC
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DAO Code to remove ALL the groups that a user belongs to Tony_VBACoder Microsoft Access Security 1 1st Jul 2004 10:02 PM
User groups in VBA Code Jim Franklin Microsoft Access 3 28th May 2004 06:26 AM
Adding Code Groups and PermissionSets through code Thomas Delrue Microsoft Dot NET Framework 2 30th Oct 2003 12:53 PM
users switch groups in code TC Microsoft Access Security 4 11th Oct 2003 02:38 PM
Groups Policy to assign user to Local Machine Power Users Groups. KA Kueh Microsoft Windows 2000 Group Policy 3 19th Sep 2003 07:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.