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!

Deny access to part of form based on MS Access built in security 2

Status
Not open for further replies.

northernbeaver

Programmer
Jul 9, 2001
164
0
0
CA
Hello everyone, cant seem to fing the solution Im looking for so I will ask for some help. I have a database (front end on local machines, backend on server) I have a mdw where I have set up three groups; Admins, User, Manager

I have a master form frmEmployee on this form there are three Pages if the current user belongs to Admin group I want them to have access to all three pages, If the user belongs to Manager, I want them to be able to see two of the three pages and the user group should only see one page on this form. I cant find the VBA command that allows me to check the current user group status.

any ideas?
 
The default workspace object contains two collections named Users and Groups, which hold an entry for each user and group account, respectively, in the current system database (workgroup file). The User objects have their own Groups collection that contains the groups the user belongs to, and likewise, the Group objects have their own Users collection that contains the users who belong to that group. So you can start from either a user or group and look up the other to see if they're associated.

For example, starting with the user and looking up his groups can be done this way:
Code:
    DbEngine.Workspaces(0).Users(CurrentUser).Groups
This expression returns a Groups collection which contains a Group object for each security group account the current user belongs to.

If you just want to know whether the user belongs to some particular group, you can write a general purpose function:
Code:
Public Function UserIsInGroup(GroupName As String) As Boolean
    Dim grp As Group

    On Error Resume Next
    Set grp = DbEngine.Workspaces(0).Users(CurrentUser).Groups(GroupName)
    If Not grp Is Nothing Then
        UserIsInGroup = True
        Set grp = Nothing
    End If
End Sub

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
RagenPryde, I presume when you are talking about pages in your post you mean a tabbed control.

If this is correct you may want to put an event on the on_change of the tab control.

From here you could check the users group and if they do not possess the required level to view a page then give them a message box informing them so and then return them to the page they can view.

You could try something like this (presuming all groups see the first page)

Private Sub YourTabControlName_Change()
Dim ws As Workspace
Dim usr As User
Dim i As Integer

Set ws = DBEngine.Workspaces(0)
Set usr = ws.Users(CurrentUser())

Select Case YourTabControlName.Value
Case 0 'First page index
'Do nothing as they can all see this
Case 1 'Second page
For i = 0 To usr.Groups.Count - 1
'********Check if in named group - run code**********
If usr.Groups(i).Name = "Admins" Or usr.Groups(i).Name = "Manager" Then
Set usr = Nothing
Set ws = Nothing
Exit For
Else
MsgBox "You do not have permission to see this page"
me.YourTabControlName.Value = 0
End If
Next
Case 2 'third page
For i = 0 To usr.Groups.Count - 1
'********Check if in named group - run code**********
If usr.Groups(i).Name = "Admins" Then
Set usr = Nothing
Set ws = Nothing
Exit For
Else
MsgBox "You do not have permission to see this page"
me.YourTabControlName.Value = 0
End If
Next
End Select
End Sub



Hope this helps
Jonathan
 
Thank you very much Rick Sprague and Jonathan. very helpfull, Ill get to trying your ideas asap.
 
An simpler alternative is to hide the pages in the Open event of the form:
Code:
    TabCtl1.Pages(1).Visible = UserIsInGroup("Managers") _
                            Or UserIsInGroup("Admins")
    TabCtl1.Pages(2).Visible = UserIsInGroup("Admins")
If all your administrators are also in the Managers group, you can leave out the Or clause.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top