How to find out if current user is in the group admin

G

Guest

Hallo

I would like to find out when i click on exit button of my access db to find
out if the user that's logged is in the admin group if yes then run some
queries if not in the admin group then exit how is this possible

I have asked this question before and the answer I got it look at the FAQ on
security since im not a VB expert it did not really help me the code i found
there which should give me what i want is this but i dont know where to put
it and how to use it from there, it would be very help full if some one can
just tell me how to get a hidden text box just to say admin if the user is
admin and if not just to say user the rest i can do. By my self

Thank you
Markus


Function faq_IsUserIngroup(strGroup As String, strUser As String) As Integer
' Return True if User is in Group, False otherwise
' This only workis if you're a member of the Admin group.
Dim ws As WorkSpace
Dim grp As GroupLevel
Dim strUsername As String

Set ws = DBEngine.Workspaces(0)
Set grp = ws.groups(strGroup)
On Error Resume Next
strUsername = ws.groups(strGroup).users(strUser).Name
faq_IsUserIngroup = (Err = 0)

End Function
 
R

Rick B

You would have to place code on a form's close event. This means you would
want to open a form with the database and leave it open (hidden) as long as
the database is open.

The code would be something like...


If IsUserIngroup(Admin) Then
Do something
Else
So Something Else
End If
 
G

Guest

Hi

Tried it does not work it gives a compile error highlights isuseringroup and
has the error sub or function not defined

Regards
Markus
 
R

Rick B

Well, have you created the module that stores that function so that you can
use it in your code?

It needs to be added to a module. The module name CAN NOT be
"IsUserIngroup" or the system will confuse the module with the function.

Rick B
 
G

Guest

HI Rick

Thank so fare but im really stupid

Would you mind taking me step by step trough this

1 I need a module What code needs to be in the module
2. Whate code do i need in on the button on my form

Thanks for you help
Markus
 
R

Rick B

Where did you find that "function"? Wherever it was, it should have
instructed you to create a new module and paste it into the module. As
previously stated, the module would need a different unique name. The
Northwinds sample database stores the common functions in a module called
"Utility Functions".

In your form, you'd paste code similar to the air code I gave you...

Private Sub Form_Close()
If IsUserIngroup(Admin) Then
Do something
Else
Do Something Else
End If
End Sub


I have not used that function, So I am not sure if you would need
If IsUserIngroup(Admin) Then

or

If IsUserIngroup("Admin") Then


Hope that helps,

Rick B
 
G

Guest

i have a module called module1 with the code below is it correct or does it
need chaning and what change does it need

Function faq_IsUserIngroup(strGroup As String, strUser As String)
As
Integer
' Return True if User is in Group, False otherwise
' This only workis if you're a member of the Admin group.
Dim ws As WorkSpace
Dim grp As GroupLevel
Dim strUsername As String
Set ws = DBEngine.Workspaces(0)
Set grp = ws.groups(strGroup)
On Error Resume Next
strUsername = ws.groups(strGroup).users(strUser).Name
faq_IsUserIngroup = (Err = 0)
End Function
 
R

Rick B

I don't know if the code is correct. Did you copy it from someplace? It
looks correct.

Have you tried to use it in a form like I suggested? Does it work?

Rick B
 
G

Guest

Its of the FAQ site from Micrsoft

Then i ask my self why it still does not work

thats the module

and the code that i use on my switchboard on close is

Private Sub Form_Close()

If IsUserIngroup(Admins) Then
DoCmd.RunMacro "Delete Old Cards"
Else
GoTo Close_Click
End If
Close_Click:
Exit Sub

End Sub

and the code thats on the Exit button is
Private Sub Exit_Click()

Forms![Switchboard]!Logoff = Now()
DoCmd.Quit

End Sub

so why is it not working Rick thanks a lot for your help so fare
 
R

Rick B

What do you mena "it does not work?"

Why don't you try to replace your code with this to see what is happening...

Private Sub Form_Close()

If IsUserIngroup(Admins) Then
Msgbox "User IS in Admins"
Else
Msgbox "User is NOT in Admins"
End If

End Sub


Markus said:
Its of the FAQ site from Micrsoft

Then i ask my self why it still does not work

thats the module

and the code that i use on my switchboard on close is

Private Sub Form_Close()

If IsUserIngroup(Admins) Then
DoCmd.RunMacro "Delete Old Cards"
Else
GoTo Close_Click
End If
Close_Click:
Exit Sub

End Sub

and the code thats on the Exit button is
Private Sub Exit_Click()

Forms![Switchboard]!Logoff = Now()
DoCmd.Quit

End Sub

so why is it not working Rick thanks a lot for your help so fare

Rick B said:
I don't know if the code is correct. Did you copy it from someplace? It
looks correct.

Have you tried to use it in a form like I suggested? Does it work?

Rick B

does
it that
you (hidden)
as look
at me
the admin if
the
 
G

Guest

It does not give any

it just exits the DB with out any messages

Rick B said:
What do you mena "it does not work?"

Why don't you try to replace your code with this to see what is happening...

Private Sub Form_Close()

If IsUserIngroup(Admins) Then
Msgbox "User IS in Admins"
Else
Msgbox "User is NOT in Admins"
End If

End Sub


Markus said:
Its of the FAQ site from Micrsoft

Then i ask my self why it still does not work

thats the module

and the code that i use on my switchboard on close is

Private Sub Form_Close()

If IsUserIngroup(Admins) Then
DoCmd.RunMacro "Delete Old Cards"
Else
GoTo Close_Click
End If
Close_Click:
Exit Sub

End Sub

and the code thats on the Exit button is
Private Sub Exit_Click()

Forms![Switchboard]!Logoff = Now()
DoCmd.Quit

End Sub

so why is it not working Rick thanks a lot for your help so fare

Rick B said:
I don't know if the code is correct. Did you copy it from someplace? It
looks correct.

Have you tried to use it in a form like I suggested? Does it work?

Rick B

i have a module called module1 with the code below is it correct or does
it
need chaning and what change does it need

Function faq_IsUserIngroup(strGroup As String, strUser As String)
As
Integer
' Return True if User is in Group, False otherwise
' This only workis if you're a member of the Admin group.
Dim ws As WorkSpace
Dim grp As GroupLevel
Dim strUsername As String
Set ws = DBEngine.Workspaces(0)
Set grp = ws.groups(strGroup)
On Error Resume Next
strUsername = ws.groups(strGroup).users(strUser).Name
faq_IsUserIngroup = (Err = 0)
End Function


:

Where did you find that "function"? Wherever it was, it should have
instructed you to create a new module and paste it into the module. As
previously stated, the module would need a different unique name. The
Northwinds sample database stores the common functions in a module
called
"Utility Functions".

In your form, you'd paste code similar to the air code I gave you...

Private Sub Form_Close()
If IsUserIngroup(Admin) Then
Do something
Else
Do Something Else
End If
End Sub


I have not used that function, So I am not sure if you would need
If IsUserIngroup(Admin) Then

or

If IsUserIngroup("Admin") Then


Hope that helps,

Rick B




HI Rick

Thank so fare but im really stupid

Would you mind taking me step by step trough this

1 I need a module What code needs to be in the module
2. Whate code do i need in on the button on my form

Thanks for you help
Markus

:

Well, have you created the module that stores that function so that
you
can
use it in your code?

It needs to be added to a module. The module name CAN NOT be
"IsUserIngroup" or the system will confuse the module with the
function.

Rick B


Hi

Tried it does not work it gives a compile error highlights
isuseringroup
and
has the error sub or function not defined

Regards
Markus

:

You would have to place code on a form's close event. This
means
you
would
want to open a form with the database and leave it open (hidden)
as
long
as
the database is open.

The code would be something like...


If IsUserIngroup(Admin) Then
Do something
Else
So Something Else
End If


Hallo

I would like to find out when i click on exit button of my
access
db
to
find
out if the user that's logged is in the admin group if yes
then
run
some
queries if not in the admin group then exit how is this
possible

I have asked this question before and the answer I got it look
at
the
FAQ
on
security since im not a VB expert it did not really help me
the
code i
found
there which should give me what i want is this but i dont know
where
to
put
it and how to use it from there, it would be very help full if
some
one
can
just tell me how to get a hidden text box just to say admin if
the
user is
admin and if not just to say user the rest i can do. By my
self

Thank you
Markus


Function faq_IsUserIngroup(strGroup As String, strUser As
String)
As
Integer
' Return True if User is in Group, False otherwise
' This only workis if you're a member of the Admin group.
Dim ws As WorkSpace
Dim grp As GroupLevel
Dim strUsername As String

Set ws = DBEngine.Workspaces(0)
Set grp = ws.groups(strGroup)
On Error Resume Next
strUsername = ws.groups(strGroup).users(strUser).Name
faq_IsUserIngroup = (Err = 0)

End Function
 
T

TC

Markus wrote:

(snip)
Function faq_IsUserIngroup(strGroup As String, strUser As String) As Integer
' Return True if User is in Group, False otherwise
' This only workis if you're a member of the Admin group.
Dim ws As WorkSpace
Dim grp As GroupLevel
Dim strUsername As String
Set ws = DBEngine.Workspaces(0)
Set grp = ws.groups(strGroup)
On Error Resume Next
strUsername = ws.groups(strGroup).users(strUser).Name
faq_IsUserIngroup = (Err = 0)
End Function

That code is terrible.

- the function should return a Boolean, not an Integer;

- you do *not* need to be a member of the Admins group;

- the Dim As GroupLevel is wrong: GroupLevel is a Report thing, not a
user/group thing;

- the function will fail with a runtime error if the specified group
does not exist.

Try this code instead. It returns True if the specified user is in the
specified group; or False if the user does not exist, or the group does
not exist, or the user is not in that group.

Public Function IsUserInGroup(strGroup as string, strUser as string) as
Boolean
dim s as string
on error resume next
s = dbengine(0).users(strUser).groups(strGroup).name
IsUserInGroup = (err.number = 0)
end function

HTH,
TC
 
G

Guest

Hi TC

Thanks for you help i have enter your code in the module but i dont
understand where should i put the code now when i run my exit command how
does the code look like im all lost now. and i dont understand the code that
you gave me how does it know who the currentuser is thats loged on dont i
need to tell it that.

Hope you can help
Markus
 
R

Rick B

Marcus:

I have told you several times how to write the code in your form. If you do
not understand that, then you should not be working with vba.

I'm not sure where you got the module from, but TC corrected that for you.
Now that you have the module built with a public function, you need to call
that function in your form's close event.

Review the previous posts for where and how to do this.

Rick B
 
G

Guest

Hi Rick

I did use the code you gave me and the form and with the code TC gave and I
just does not work I even tried with the last post you send with using the
message box it does not even show them Here is the code again maybe im doing
some thing wrong some where maybe it does not load the module what do you
need to do to load it or does it load automatically

Module1
Public Function IsUserInGroup(strGroup As String, strUser As String) As
Boolean
Dim s As String
On Error Resume Next
s = DBEngine(0).Users(strUser).Groups(strGroup).Name
IsUserInGroup = (Err.Number = 0)
End Function

Form Switchboard

On Close
Private Sub Form_Close()

If IsUserInGroup(Admins) Then
MsgBox "User IS in Admins"
Else
MsgBox "User is NOT in Admins"
End If

End Sub

I have try (Admin), ("Admin"), (Admins) and ("Admins")

and then i have the exit button which i click to close the DB which should
then run the on close event if i am right

Private Sub Exit_Click()

Forms![Switchboard]!Logoff = Now()
DoCmd.Quit

End Sub

So i am doing it like you guys keep on telling me it just does not work it
just closed the DB and when i open it holding down the shift key and then
open the form switchboard and click on the Exit button it give the error

Compile error
Byref argument type mismatch

If it is possible I could glad send you the DB and you could maybe have a
look at it see where I am going wrong

I really appreciate all your help and I really need to get this working we
do not have to do it by email if you don’t want I could just upload it to a
shard web site for you to download and once you found it you could maybe
upload it the same way again.

Regards
Markus
 
J

Jeff Conrad

Markus,

So far so good, but you have left out a VERY important element
when you call the code. If you notice TC's code it requires two
parameters to be passed into it: the name of the group you wish
to check AND the user name you wish to check. You have left
out the user name parameter so Access does not know "who"
you want to check!

If you want to check to see if the currently logged on user is a
member of the Admins group then do this:

If IsUserInGroup("Admins",CurrentUser()) Then
MsgBox "User IS in Admins"
Else
MsgBox "User is NOT in Admins"
End If

--
Jeff Conrad
Access Junkie
Bend, Oregon
Bend's idea of 'Spring Break':
http://www.tripcheck.com/RoadCams/RoadCams_Bend.htm
 
J

Joan Wild

Markus said:
Module1
Public Function IsUserInGroup(strGroup As String, strUser As String)
As Boolean
Dim s As String
On Error Resume Next
s = DBEngine(0).Users(strUser).Groups(strGroup).Name
IsUserInGroup = (Err.Number = 0)
End Function

Form Switchboard

On Close
Private Sub Form_Close()

Change the following line to
If IsUserInGroup(Admins) Then
If IsUserInGroup("Admins", CurrentUser()) then
 
G

Guest

Thanks a lot Jeff its working perfectly now.

Rick and TC thanks a lot for your help could not have done it with out yours
help

Markus

Jeff Conrad said:
Markus,

So far so good, but you have left out a VERY important element
when you call the code. If you notice TC's code it requires two
parameters to be passed into it: the name of the group you wish
to check AND the user name you wish to check. You have left
out the user name parameter so Access does not know "who"
you want to check!

If you want to check to see if the currently logged on user is a
member of the Admins group then do this:

If IsUserInGroup("Admins",CurrentUser()) Then
MsgBox "User IS in Admins"
Else
MsgBox "User is NOT in Admins"
End If

--
Jeff Conrad
Access Junkie
Bend, Oregon
Bend's idea of 'Spring Break':
http://www.tripcheck.com/RoadCams/RoadCams_Bend.htm

Hi Rick

I did use the code you gave me and the form and with the code TC gave and I
just does not work I even tried with the last post you send with using the
message box it does not even show them Here is the code again maybe im doing
some thing wrong some where maybe it does not load the module what do you
need to do to load it or does it load automatically

Module1
Public Function IsUserInGroup(strGroup As String, strUser As String) As
Boolean
Dim s As String
On Error Resume Next
s = DBEngine(0).Users(strUser).Groups(strGroup).Name
IsUserInGroup = (Err.Number = 0)
End Function

Form Switchboard

On Close
Private Sub Form_Close()

If IsUserInGroup(Admins) Then
MsgBox "User IS in Admins"
Else
MsgBox "User is NOT in Admins"
End If

End Sub

I have try (Admin), ("Admin"), (Admins) and ("Admins")

and then i have the exit button which i click to close the DB which should
then run the on close event if i am right

Private Sub Exit_Click()

Forms![Switchboard]!Logoff = Now()
DoCmd.Quit

End Sub

So i am doing it like you guys keep on telling me it just does not work it
just closed the DB and when i open it holding down the shift key and then
open the form switchboard and click on the Exit button it give the error

Compile error
Byref argument type mismatch

If it is possible I could glad send you the DB and you could maybe have a
look at it see where I am going wrong

I really appreciate all your help and I really need to get this working we
do not have to do it by email if you don't want I could just upload it to a
shard web site for you to download and once you found it you could maybe
upload it the same way again.

Regards
Markus
 

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

Top