View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Alok[_2_] Alok[_2_] is offline
external usenet poster
 
Posts: 5
Default UserName Property

On Dec 5, 10:25 am, AccessHelp
wrote:
Good morning,

I have a command button in a workbook to run a macro. However, when a user
clicks on the button, the macro will check first automatically whether the
user who clicks on it has an authority to run the macro. If not, the user
will receive a message "Access denied.".

So I came up with the following code:

If Application.UserName < "John Doe" Then
Msgbox "Access denied."
Else
run the remainder of code......
End If

2 questions:

1. What does the UserName Property check against with? Does it check
against with Windows user name or computer user name? When I did the code
"Msgbox Application.UserName", I got my first and last names.
2. I will have more than one authorized users, and instead of having the
code "If Application.UserName < "John Doe" Or If Application.UserName <
"John Smith" Or ......", can you help me with better coding?

Thanks.


Not sure about the first question, but for the second I would make a
"IsAuthorizedUser(username As String) As Boolean" function, and call
it like

If Not IsAuthorizedUser(Application.UserName) Then
Msgbox "Access denied."
Else
run the remainder of code......
End If

If there aren't too many authorized users, I would think you could
structure IsAuthorizedUser as follows:

Function IsAuthorizedUser(username As String) As Boolean
Select Case username
Case "Joe Smith"
IsAuthorizedUser = True
Case "Jane Doe"
IsAuthorizedUser = True
Case Else
IsAuthorizedUser = False
End Select
End Function

But whatever you think is readable would work.