Really, why use any code at all?
What you should do is simply make and create those user groups.
For example, in my last application I make the following groups:
Basic - Basic Rides user
DailyReports - Daily Reports
SeasonReports - Seasonal Reports
RidesGroups - Allow creation of Corporate Groups
InvForward - Can forward date invoice payments
InvBackWard - Can back date invoices payments
InvDelete - Can delete invoice payments
AddTours - Add Tours, Rooms, Wholesale Tour Pricing
Admins - Administrator (Add users, change passwords)
Then, when I add a new user to the system, I simply select from the above
list of groups that I want the user to be able to do. For example, in the
above I have:
Allow creation of Corporate Groups
That simply means that the user can open and use the Corporate Groups form.
If I go into the security setup, I simply set the permissions of the
"Corporate Group" FORM to the above user group. YOU SHOULD NEVER set
permissions of forms, reports etc to individual users. (and, it seems you
have got your system setup using this correct approach...good for you!).
The reason why you don't want to set individual users to any particular form
is that it becomes a nightmare to add a new user to the system. A
application can typically have a 100 or more forms...and trying to figure
out what form(s) a single user can use will be a maintains night mare.
So, the simple solution is to create some nice user "groups" and then assign
the user to those groups. It is all those groups where you do the hard work
of assigning of what group belongs to what form. Further, once you do this
assignment, then if a user does NOT have permissions to use a form, the you
don't even have to write ONE line of code (this is the whole idea of
implementing security in the first place). If you set the security of the
form to that group, then any user that tries to open or use that form MUST
be a member of that security group. So, really, you don't want to write code
to check, or test every form that you load. This is even more handy for
reports. Of course, about the only downfall of writing no code is that you
get a rather lousily error message. However, if you are building custom tool
bars/menu bars..then the code free solution is nice.
Also, to be fair...it looks like you done your security the right way, and
are simply asking how to check if a user belongs to a particular user group.
I often do this, but NOT usually for forms..since they each can be assigned
to a user group anyway.
However, I never did think that the built in security system is user
friendly from a user point of view. So I wrote my on security manager. You
can see some screen shots of it in action at:
However, note while I wrote my own security manager, I certainly do use and
rely on the built-in security to decide who can load and use what forms. I
do this since it saves tons of code, and in fact using security means you
can control with whole process with NO code at all!
Anyway, the code example posted could be used like:
if IsInGroup(CurrentUser(),"MasterUser") = true then
bla bla bla
end if
Or, in my above example, I have that the user can forward date invoices. So,
in this case I DO NEED to use the IsInGroup, since security is only assigned
to whole form. So, at this point once again I would use code. It looks like:
If (IsInGroup(CurrentUser(), "InvForward") = False) And _
(IsInGroup(CurrentUser(), "Admins") = False) Then
Beep
MsgBox "Forward dating of a invoice is not allowed", vbExclamation
Cancel = True
End If
You can see in the above example, the user must be a member of InvForward
group OR can be a member of admins.