Adding users to security group using DAO

G

Guest

Hi.
Wanting to add users to security groups, I purchased a book from wrox that
gives me the following DAO function:

Public Function CreatuserAccount(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 CreatuserAccountErr

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreatuserAccountErr:
Set usr = Nothing
Set wrk = Nothing

End Function

This code looks great. But, I don't know how to use it. I created a module
and put the function in. But, what's the next step? Any help would be
appreciated. Thank you.
 
G

Guest

Well it is a function that requires 3 input variables: strUserName, strPID
and strPassword.

If you wish to use it from the immediate window, you simply enter
? CreatuserAccount("SomeUserName","AUniquePID","APasswordForTheUserAccount")

However, I'm assuming you wish to use it from within your db. You'll need
to create a from with 4 textbox controls and a command button. Yes 4, you
should make 2 password fields to confirm the entry.

txtbox1 - named: txtUserName
txtbox2 - named: txtPID
txtbox3 - named: txtPwd1
txtbox4 - named: txtPwd2
cmdbutton - named: cmdCreateUser

Then you need to create an OnClick event to run the code you posted while
passing the values your user entered. Something like:

'*******
'Ensure all the fields were completed
If isnull(me.txtUserName) or isnull(me.txtPID) or isnull(me.txtPwd1) or
isnull(me.txtPwd2) then
msgbox "Not all the mandatory fields were completed. Please entered all
the required information and try again!"
Exit Sub
End

' Confirm password
if me.txtPwd1<>me.txtPwd2 then
msgbox "Your password doe not match the confirmation entry. Please
re-enter your password and try again"
me.txtPwd1=""
me.txtPwd2=""
me.txtPwd1.setfocus
Exit Sub
End if

'Create the user
call CreatuserAccount(me.txtUserName,me.txtPID,me.txtPwd1)
'*******

I have omitted error handling, but you should ensure to include some!
 
G

Guest

I just noticed that the code you posted is for creating a new user account
and not adding a user to a group.

Please find below various function relavent to user account (creating,
deleting, adding to groups,...). however, I cannot take the credit for
creating the code

*******
'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
*******
--
Hope this helps,

Daniel P
 
G

Guest

Thank you so much.

Daniel said:
I just noticed that the code you posted is for creating a new user account
and not adding a user to a group.

Please find below various function relavent to user account (creating,
deleting, adding to groups,...). however, I cannot take the credit for
creating the code

*******
'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
*******
 
S

Scott McDaniel

Hi.
Wanting to add users to security groups, I purchased a book from wrox that
gives me the following DAO function:

Please don't crosspost. I just answered this question in the security newsgroup, with an almost identical answer as
given by Daniel, and thus wasted my time. If you feel you must post this to more than one group (and that's rarely
necessary) then multipost so that answers given in any newsgroup can be viewed.
Public Function CreatuserAccount(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 CreatuserAccountErr

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreatuserAccountErr:
Set usr = Nothing
Set wrk = Nothing

End Function

This code looks great. But, I don't know how to use it. I created a module
and put the function in. But, what's the next step? Any help would be
appreciated. Thank you.

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G

Guest

Dear Scott,

Sorry about that. After posting in security group, I could not find my post
for some reason. So, I thought my post wasn't posted. It won't happen again.
 
S

Scott McDaniel

Dear Scott,

Sorry about that. After posting in security group, I could not find my post
for some reason. So, I thought my post wasn't posted. It won't happen again.

Not a problem ... and I'm glad you got your issue fixed!!

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
C

crystal

ÊÖ»ú
Jae said:
Hi.
Wanting to add users to security groups, I purchased a book from wrox that
gives me the following DAO function:

Public Function CreatuserAccount(strUserName As String, _
strPID As String, strPassword As String)

Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreatuserAccountErr:
Set usr = Nothing
Set wrk = Nothing

End Function

This code looks great. But, I don't know how to use it. I created a module
and put the function in. But, what's the next step? Any help would be
appreciated. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top