VBA managing USER's Rights/Access

blu_macey

Registered User.
Local time
Today, 08:46
Joined
Apr 1, 2015
Messages
31
Hello everyone,

Here I am again seeking for your expert's advice.
In my database there are three roles namely
1. Accountant
2. Bookkeeper
3. Cashier

I have a Main Page Menu form with three buttons, specifically for the above mentioned roles.

My code goes like this:

Code:
Private Sub Form_Load()

    txtUser.Value = strUser
    txtRole.Value = strRole

    txtFocus.SetFocus

    'Set Enabled state of command button based on User Role
    
    If strRole = "Accountant" Then
    
        cmdMenu1.Enabled = True
        cmdMenu2.Enabled = True
        cmdMenu3.Enabled = True
        
    ElseIf strRole = "Bookkepeer" Then
    
        cmdMenu1.Enabled = False
        cmdMenu2.Enabled = True
        cmdMenu3.Enabled = False
        
    ElseIf strRole = "Cashier" Then
    
       cmdMenu1.Enabled = False
       cmdMenu2.Enabled = False
       cmdMenu3.Enabled = True
  End If
  
  
End Sub

Unfortunately Nothing Happens, meaning each roles can still access all three buttons.
But if My code is like this:

Code:
Private Sub Form_Load()

    txtUser.Value = strUser
    txtRole.Value = strRole

    txtFocus.SetFocus

    'Set Enabled state of command button based on User Role
    
    If strRole = "Accountant" Then
    
        cmdMenu1.Enabled = True
    Else
         cmdMenu1.Enabled = False
 
  End If
  
  
End Sub
Then its working, for bookkeeper and the accountant.
may I know where did I go wrong?
For what I only want is to set a super user for the accountant, meaning having an access rights to all 3 cmdMenu buttons and setting the bookkeeper rights only at cmdMenu2 and Cashier at cmdMenu3.

Looking again forward to here from you.

Thank you and best regards.

Cheers!
Chino
 
Last edited:
Got the answer to my question here...

http://bytes.com/topic/access/answers/845378-nested-if-elseif-syntax

final Code as edited..

Code:
Private Sub Form_Load()

    txtUser.Value = strUser
    txtRole.Value = strRole

    txtFocus.SetFocus

    'Set Enabled state of command button based on User Role
    
    If strRole = "Accountant" Then
    
        cmdMenu1.Enabled = True
        cmdMenu2.Enabled = True
        cmdMenu3.Enabled = True
        Exit Sub
    End If
    If strRole = "Bookkeeper" Then
    
        cmdMenu1.Enabled = False
        cmdMenu2.Enabled = True
        cmdMenu3.Enabled = False
        Exit Sub
    End If
    If strRole = "Cashier" Then
    
        cmdMenu1.Enabled = False
        cmdMenu2.Enabled = False
        cmdMenu3.Enabled = True
        Exit Sub
    End If
  
End Sub

Sorry to disturb yah all =)
 
I struggle to see why the first example failed, to be honest.

the only thing that occurs to me is that if strrole is none of the above, then the buttons won't get set - but the third example is the same.

I would have stepped through the first example with a breakpoint
 
I struggle to see why the first example failed, to be honest.

the only thing that occurs to me is that if strrole is none of the above, then the buttons won't get set - but the third example is the same.

I would have stepped through the first example with a breakpoint

Yeah, for sure it does not look like it was the code. The edited code is functionally equivalent.

Best,
Jiri
 
Hello Dave and Jiri,

Sorry I haven't posted the error message for the 1st example, my bad,
Anyway when running the code it says, "Compile error: Block if without End If"
That is why I look for a way around.

Thank you for sharing your ideas, I'm learning a lot :)

Cheers!
Chino
 

Users who are viewing this thread

Back
Top Bottom