Hide Button Based on User Role

Megaduck

Member
Local time
Yesterday, 20:23
Joined
Apr 24, 2020
Messages
30
I have been trying to get this to work all day it seems. Not sure what I am doing wrong.

I have a table [Employee List]. In that table I have [Employee Name] and their [Role] as fields.

Role has either Admin or User as options.

On the login form, I have a Combo box with their name, which they select based on [Employee List]. No password or anything. Real simple.

This sets a TempVar of [CurrentUserID] which I use to auto populate a form with. That part works fine.

What I want to do is show a button on the main form [Dashboard] based on that user's role. Specifically, I want to show it if they are admin.

This is what I have:

Code:
Private Sub Form_Open(Cancel As Integer)

UserLevel = DLookup("[Role]", "[Employee List]", "[Employee Name]='" & [TempVars]![CurrentUserID] & "'")

If UserLevel = "Admin" Then
    Me.Adminbtn.Visible = True
Else
    Me.Adminbtn.Visible = False
    
End If

End Sub

Any idea what I am doing wrong? Lol.
 
Hi. Where is UserLevel variable declared? Why not use a TempVar for it as well (like you did with the user's name)?
 
Why not walk through the code with F8 ?
 
Hi. Where is UserLevel variable declared? Why not use a TempVar for it as well (like you did with the user's name)?

Wow.... I figured out what I did (or rather, didn't do) based on your reply. I added "Dim UserLevel As String" and it works fine.
I've been staring at this for far too long today apparently....

Thank you!
 
The code looks OK but Form_Open is the wrong event for this code.
Try moving it to the Form_Load event
 
Last edited:
Wow.... I figured out what I did (or rather, didn't do) based on your reply. I added "Dim UserLevel As String" and it works fine.
I've been staring at this for far too long today apparently....

Thank you!
Hi. Glad to hear you got it to work. Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom