Online Book Reader

Home Category

Access Cookbook - Ken Getz [216]

By Root 2020 0
the form's MenuBar property to point to different sets of menu macros. We have done all of this in the sample frmSwitchboard form. The runtime customizations to frmSwitchboard are summarized in Table 10-15.

The code that drives this customization process for frmSwitchboard is shown here:

Private Sub Form_Open(Cancel As Integer)

' Adapt switchboard to match level of logged in user.

' Because users can be members of more than one group,

' you need to check membership in decreasing order

' starting with the highest-level group.

If acbAmMemberOfGroup("Managers") Then

Me.cmdManager1.Visible = True

Me.cmdManager2.Visible = True

Me.cmdProgrammer1.Visible = False

Me.cmdProgrammer2.Visible = False

Me.cmdDefault1.Visible = False

Me.cmdDefault2.Visible = False

Me.cmdClose.Visible = True

Me.lblMenu.Caption = "Manager Main Menu"

ElseIf acbAmMemberOfGroup("Programmers") Then

Me.cmdManager1.Visible = False

Me.cmdManager2.Visible = False

Me.cmdProgrammer1.Visible = True

Me.cmdProgrammer2.Visible = True

Me.cmdDefault1.Visible = False

Me.cmdDefault2.Visible = False

Me.cmdClose.Visible = False

Me.lblMenu.Caption = "Programmer Main Menu"

Else

Me.cmdManager1.Visible = False

Me.cmdManager2.Visible = False

Me.cmdProgrammer1.Visible = False

Me.cmdProgrammer2.Visible = False

Me.cmdDefault1.Visible = True

Me.cmdDefault2.Visible = True

Me.cmdClose.Visible = False

Me.lblMenu.Caption = "Default Main Menu"

End If

End Sub

Discussion


By default, the form is saved with the least-secure options set; if anything goes wrong, this provides a little extra assurance. When any user opens frmSwitchboard, the Load event procedure is called, and the form's look and feel is customized on the fly. Group membership is determined using the acbAmMemberOfGroup function found in basGroupMember:

Public Function acbAmMemberOfGroup(strGroup As String)

Dim wrk As DAO.Workspace

Dim usr As DAO.User

Dim strTest As String

Set wrk = DBEngine.Workspaces(0)

' Refresh collections to stay in synch with

' Access UI

wrk.Users.Refresh

wrk.Groups.Refresh

' Set up pointer to current user

Set usr = wrk.Users(CurrentUser( ))

' Handle errors in line

On Error Resume Next

' If any property of the Groups collection

' using the passed-in group works then we're

' a member. Otherwise an error will occur

' and we can assume we are not a member.

strTest = usr.Groups(strGroup).Name

acbAmMemberOfGroup = (Err.Number = 0)

Err.Clear

End Function

This function is simple: it determines if a user is a member of a group by setting a pointer to the Users collection of the current user and then attempts to get the name of the group in the Groups collection of that user. If this fails, the user is not a member of the group in question. If it succeeds, the user must be a member of the group. See the Solution in Recipe 10.5 for more details on the programmatic manipulation of user and group collections.

We could have based the form customizations on the name of the current user using the built-in CurrentUser function, but this requires us to consider each user individually, which should be avoided if possible. It's much easier to manage groups of users rather than individual users. Still, you can always add more tests to the If...Then statement in the Load event procedure.

TIP

Versions of Access prior to Access 95 did not allow a user to check group membership unless the user was also a member of the Admins group, but the recent versions of Access allow this.

It's important that you include an Else clause in the If...Then statement of the Load event procedure to handle users who are not members of any of the groups for which you have tested. In the sample event procedure, we have tested for membership in only the Managers and Programmers groups. Any users who are not members of either group are handled by the Else clause.

You can use this technique to alter any runtime property in response to the user's group membership, including:

Whether certain menu items appear.

Whether certain controls are visible, and therefore active.

Return Main Page Previous Page Next Page

®Online Book Reader