User and Group Operations

Before you can assign permissions that allow the user to access any of the Access or Jet objects, you must add the users to one or more groups.

There are two ways you can do this: by adding the user to the group, or by adding the group to the user. Although the following two procedures achieve exactly the same end, they demonstrate how to do it:

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

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

Similarly, if you want to delete a user from a group, you can delete the user's entry from the Groups collection, or delete the group from the Users collection. For simplicity, we'll only show you one method.

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

0 -1

Responses

  • Iolanda
    How to get user group with VBA in access 2003?
    7 years ago

Post a comment