Hide Button Based on User Role (1 Viewer)

Megaduck

Member
Local time
Today, 03:15
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:15
Joined
Oct 29, 2018
Messages
21,467
Hi. Where is UserLevel variable declared? Why not use a TempVar for it as well (like you did with the user's name)?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:15
Joined
Sep 21, 2011
Messages
14,267
Why not walk through the code with F8 ?
 

Megaduck

Member
Local time
Today, 03:15
Joined
Apr 24, 2020
Messages
30
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!
 

isladogs

MVP / VIP
Local time
Today, 08:15
Joined
Jan 14, 2017
Messages
18,216
The code looks OK but Form_Open is the wrong event for this code.
Try moving it to the Form_Load event
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 00:15
Joined
Oct 29, 2018
Messages
21,467
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

Top Bottom