Test for User?

  • Thread starter Thread starter RobG3381
  • Start date Start date
R

RobG3381

Is there a way to test for a certain user or a certain group from
within the code? What I'm trying to do is if users of a certain group
log into the DB, then access will brings up form 1. If they're not,
then it brings up form 2. I'm using Access Workgroup Security.

Thanks,
Rob
 
This function will return True if the specified user is in the specified
group ...

Public Function IsUserInGroup(ByVal UserName As String, _
ByVal GroupName As String) As Boolean

Dim usr As DAO.User
Dim grp As DAO.Group
Dim Result As Boolean

For Each usr In Workspaces(0).Users
If usr.Name = UserName Then
For Each grp In usr.Groups
If grp.Name = GroupName Then
Result = True
Exit For
End If
Next grp
Exit For
End If
Next usr

IsUserInGroup = Result

End Function

Example of use, in Immediate Window ...

? IsUserInGroup(CurrentUser(), "Admins")
True
 
Thank You Brendan. I entered this into the DB and gave it a try. For
some reason, it gets to the "For Each usr In Workspaces(0).Users" line
and skips down, thus always making the function false. I also added a
Workspaces(0).Users.Count, which returns 0. Any suggestions as to what
may be wrong? Thank you in advance.

-Rob
 
You're not trying to use it in an ADP, are you? That won't work, ADPs do not
use JET security.
 
I'm afraid I'm at a loss, Rob. I don't know how Workspaces(0).Users.Count
can return 0 in an MDB, as I have always understood that every workgroup
contains at least one user - the built-in Admin user.
 
Not sure if this helps...but while trying to troubleshoot, I added the
following three lines above the "For Each" statement and got the
following results:

MsgBox Workspaces(0).UserName
MsgBox Workspaces(0).Name
MsgBox Workspaces(0).Users.Count

Admin
#Deafult Workspace#
0

I logged into the dataase as GatesR (not admin), but with admin
rights...why would it return the username as Admin and not GatesR?
 
I'm afraid I'm still at a loss, Rob. Could you describe how and where you
are trying to use the code?
 
When a user logs in, I want them to enter some text into another
form/table. But not everyone...just certain users. So I made this
newer form my main form and on the open form, I entered the
following..and placed your code as a seperate proc.

MsgBox IsUserInGroup(CurrentUser(), "Users")

Thanks again for all your help.
 
Is this what you need:

if isuseringroup(currentuser(), "users") then
' current user is in Users group.
docmd.openform "form_A"
else
' current user is NOT in Users group.
docmd.openform "form_B"
endif

PS. Normally, /all/ users are members of the Users group. Yes?

HTH,
TC [MVP Access]
 
That's the problem though, the isuseringroup(currentuser(), "users")
always returns false because no matter what group I put into the groups
(admins, users, etc)...the workspaces(0).Users.Count returns 0 and I
can't figure out why it is always returning a false and count a 0.
 
Oh, now I have looked at the code!

Change it to this:

Public Function IsUserInGroup(UserName As String, _
GroupName As String) As Boolean
dim s as string
on error resume next
s = dbengine(0).groups(GroupName).users(UserName).name
IsUserInGroup = (err.number = 0)
End Function

HTH,
TC [MVP Access]
 

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

Back
Top