User Access Menu

B

Bill

I have a question... I have created a simple custom menu that calls a macro
and opens a report or form (etc). One item on the menu I want to restrict to
a certain level of user. I have a user level defined in table L_User which
will grant either NONE, READ, EDIT or ADMIN access.

How can I make this one button check the users level and if the level is not
sufficient deny access.

My user name is defined by pulling the window login name. I have not setup
access security or user access via the access security. That seems really
confusing to me.

Thanks for the help,

Bill
 
S

Stuart McCall

Bill said:
I have a question... I have created a simple custom menu that calls a macro
and opens a report or form (etc). One item on the menu I want to restrict
to
a certain level of user. I have a user level defined in table L_User
which
will grant either NONE, READ, EDIT or ADMIN access.

How can I make this one button check the users level and if the level is
not
sufficient deny access.

My user name is defined by pulling the window login name. I have not
setup
access security or user access via the access security. That seems really
confusing to me.

Thanks for the help,

Bill

To call code from a commandbar (I presume that's what you mean by custom
menu) you need a function in a standard module. This function should check
the user's login and only execute code if you have a match for the required
user level. Something like:

Public Function CheckUser()
Dim strLevel As String

strLevel = Dlookup("UserLevel", "tblUserLevels", "UserName = '" &
strUserName & "'")
Select Case strLevel
Case "NONE"
MsgBox "Access denied."
Case "READ"
MsgBox "Read-only access."
Case "EDIT"
MsgBox "Read/Write access."
Case "ADMIN"
MsgBox "Full rights"
Case Else
End Select
End Function

Then replace the appropriate MsgBox call with your code (or a call to it).
strUserName should contain the user's login name.

The commandbar's button should have an Action property of:

=CheckUser()
 
B

Bill

Thanks Stuart. In looking at this I really just want it to verify the user
level is admin and if so call the table. If the user level is anything other
than admin, it can do nothing or call the message script at that time.

I'm such a novice I'm having a hard time understanding how to implement your
answer to meet my needs.

Thx
 
B

Bill

Stuart,

I really appreciate the help, I just dont know how to apply it. If anyone
can help me apply this I would be greatful!

Thanks
 
B

Bill

Ok so I've progressed a little. I figured out how to create a module with
the public function as described. Then I created the event to call the
module =CheckUser()

Now in your writing you mentioned:
Then replace the appropriate MsgBox call with your code (or a call to it).
strUserName should contain the user's login name.

This is where I'm stuck.
1. I need to call a table to open if the user level is Admin.
2. strUserName should contain the user's login name?? I'm calling the
windows login name via the following function:
Function GetWindowsUser() As String
' Calls a windows API to retrieve the user name of the
' account that is currently logged on windows.
Dim lngLen&, sUserName$

'Set string to pass
sUserName = String$(254, 0)

'Set string length to pass
lngLen = 255

'Call api
If GetCurrentUser(sUserName, lngLen) <> 0 Then
'A string was returned (user name)
GetWindowsUser = Left$(sUserName, lngLen - 1)
Else
'No user name was returned
GetWindowsUser = ""
End If

End Function

So what goes in strUserName??

I really appreciate anyones help on this. Thanks! Bill
 
S

Stuart McCall

Bill said:
Stuart,

I really appreciate the help, I just dont know how to apply it. If anyone
can help me apply this I would be greatful!

Thanks

Well first we need to know whether I was correct in my interpretation of
"custom menu". Is this a commandbar?
In the example code I posted, you need to change the Dlookup arguments to
your names, then create a new standard module (ie not a class module) and
paste in the function CheckUser (or something like it). DON'T name the
module "CheckUser". Call it, say, modCheckUser.

From that point on we need an answer to my question above, as the rest of it
will differ depending on your answer.
 
S

Stuart McCall

PS I posted this before seeing your latest post, so the posts crossed. I'll
reply tomorrow (it's far too late here in UK at the moment)
 
B

Bill

Hi Stuart. Thank you for this. I see you last post and I appreciate you
looking at this for me. I am calling a menu list to open via a macro at
startup. While I would love to be able to create custom comand bars (across
the top) I'm just not there yet with my programming knowledge (or better said
lack there of).

So I have a menu where I have an icon which looks like a button and text
along side it. When the user clicks the button I want to call this function
and evaluate the user level. If the level is Admin then it would open the
table L_Users. If it is anything other than admin, it can display a message
stating "Restricted Access".

Thanks again for this help,

Bill
 
S

Stuart McCall

Bill said:
Hi Stuart. Thank you for this. I see you last post and I appreciate you
looking at this for me. I am calling a menu list to open via a macro at
startup. While I would love to be able to create custom comand bars
(across
the top) I'm just not there yet with my programming knowledge (or better
said
lack there of).

So I have a menu where I have an icon which looks like a button and text
along side it. When the user clicks the button I want to call this
function
and evaluate the user level. If the level is Admin then it would open the
table L_Users. If it is anything other than admin, it can display a
message
stating "Restricted Access".

Thanks again for this help,

Bill

Ok, because we're working with a commandbutton on a form, we don't need the
function. The code can go in the button's Click event. In the code below I'm
assuming that the field in L_Users that stores the user level is called
UserLevel and the field for the employee name is UserName. Wherever it is
you obtain the Windows login name, assign it to a variable called
strUserName.

1. Open your menu form in design view
2. Select the relevant button
3. Go to the property sheet and select the Event tab
4. Click on the On Click property and type [
(Access will expand this to [Event Procedure])
5. Click the build button (the button with 3 dots)
(Access will build a 'skeleton' procedure for you)
6.Paste the following code inside the procedure:

Dim strLevel As String

strLevel = Dlookup("UserLevel", "L_Users", "UserName = '" & strUserName &
"'")
If strLevel <> "ADMIN" Then
MsgBox "Restricted Access"
Exit Sub
End If
DoCmd.OpenTable "L_Users"

Hope this gets you off the ground

PS Sorry for the delay. Yesterday was 'mission impossible'.
 
B

Bill

Thank you very much for your help!!!! I changed it a little to make it work.
Here's what I did:

Private Sub Edit_User_Menu_Click()
Dim strLevel As String
Init_Globals ' initialize the global variables
strUserName = Environ("Username") ' get the username of person logged into
computer
strLevel = DLookup("Access_Level", "L_Users", "UserName = '" & strUserName &
"'") 'Compare Username with user access level
If strLevel <> "Admin" Then
MsgBox "Restricted Access"
Exit Sub
End If
DoCmd.OpenTable "L_Users"

End Sub


Thanks again for your help.


Stuart McCall said:
Bill said:
Hi Stuart. Thank you for this. I see you last post and I appreciate you
looking at this for me. I am calling a menu list to open via a macro at
startup. While I would love to be able to create custom comand bars
(across
the top) I'm just not there yet with my programming knowledge (or better
said
lack there of).

So I have a menu where I have an icon which looks like a button and text
along side it. When the user clicks the button I want to call this
function
and evaluate the user level. If the level is Admin then it would open the
table L_Users. If it is anything other than admin, it can display a
message
stating "Restricted Access".

Thanks again for this help,

Bill

Ok, because we're working with a commandbutton on a form, we don't need the
function. The code can go in the button's Click event. In the code below I'm
assuming that the field in L_Users that stores the user level is called
UserLevel and the field for the employee name is UserName. Wherever it is
you obtain the Windows login name, assign it to a variable called
strUserName.

1. Open your menu form in design view
2. Select the relevant button
3. Go to the property sheet and select the Event tab
4. Click on the On Click property and type [
(Access will expand this to [Event Procedure])
5. Click the build button (the button with 3 dots)
(Access will build a 'skeleton' procedure for you)
6.Paste the following code inside the procedure:

Dim strLevel As String

strLevel = Dlookup("UserLevel", "L_Users", "UserName = '" & strUserName &
"'")
If strLevel <> "ADMIN" Then
MsgBox "Restricted Access"
Exit Sub
End If
DoCmd.OpenTable "L_Users"

Hope this gets you off the ground

PS Sorry for the delay. Yesterday was 'mission impossible'.
 
S

Stuart McCall

Bill said:
Thank you very much for your help!!!! I changed it a little to make it
work.
Here's what I did:

Private Sub Edit_User_Menu_Click()
Dim strLevel As String
Init_Globals ' initialize the global variables
strUserName = Environ("Username") ' get the username of person logged
into
computer
strLevel = DLookup("Access_Level", "L_Users", "UserName = '" & strUserName
&
"'") 'Compare Username with user access level
If strLevel <> "Admin" Then
MsgBox "Restricted Access"
Exit Sub
End If
DoCmd.OpenTable "L_Users"

End Sub


Thanks again for your help.
<snip>

You're welcome. Glad we got you going.

FYI environment variables are notoriously unreliable. You'd be better off
using the Windows API to get your login name instead. This link will show
you how:

http://www.smccall.demon.co.uk/Windows.htm#NetUser
 

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