Show Button Only for Admins (1 Viewer)

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
Hi

I want to hide a form button (DeleteBtn) for all users except admins.

I have a user login table (tblEmployees) with the following fields:

EmpID
UserName
UserPassword
UserAccess

I have tried to implement VBA found on various sites but none are working for me. Either the button stays visible to all users and admin or it's invisible to both. I have tried setting the default visible property to both Yes and No but this doesn't appear to make any difference with any code I've tried already.

I want to make the button visible only to those users who have "Admin" access (in UserAccess).

Could someone please steer me in the right direction. Thanks.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:34
Joined
Aug 30, 2003
Messages
36,123
Why don't you post the code you're trying to implement and we'll fix it?
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
Why don't you post the code you're trying to implement and we'll fix it?

Below is the latest code I tried, without success:

Code:
Private Sub Form_Open(Cancel As Integer)

If UserAccess = "Admin" Then
    Me.DeleteBtn.Visible = True
    Else
    Me.DeleteBtn.Visible = False
End If
  
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:34
Joined
Aug 30, 2003
Messages
36,123
That code looks okay. What exactly is UserAccess and how is it populated?
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
UserAccess is the table field showing the access permission level for the user, either "Admin" or "User". It's populated via a form.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:34
Joined
Aug 30, 2003
Messages
36,123
You can't use a table field that way, unless it's part of the record source of the form this code is in. Many implementations of user security use a recordset or DLookup() to pull the access level of the logged in user and store it in a global variable, hidden form control, or TempVar.
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
You can't use a table field that way, unless it's part of the record source of the form this code is in. Many implementations of user security use a recordset or DLookup() to pull the access level of the logged in user and store it in a global variable, hidden form control, or TempVar.

How would I do that?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:34
Joined
Sep 12, 2006
Messages
15,634
that would work but given this

Code:
If UserAccess = "Admin" Then
    Me.DeleteBtn.Visible = True
    Else
    Me.DeleteBtn.Visible = False
End If


UserAccess would need to be a function in a module that checked whether the user was an admin or not.

You would also need a user to log in on starting the app, so that you could detewrime his level of access based in his name and password.

I would probablty rewrite the code to return a boolean, or even a numeric value

Code:
'within the form
    Me.DeleteBtn.Visible = AdminUser  'result of the function below

Code:
function AdminUser() as boolean
adminuser = "code to determine the appropriate access level"
End If
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
that would work but given this

Code:
If UserAccess = "Admin" Then
    Me.DeleteBtn.Visible = True
    Else
    Me.DeleteBtn.Visible = False
End If


UserAccess would need to be a function in a module that checked whether the user was an admin or not.

You would also need a user to log in on starting the app, so that you could detewrime his level of access based in his name and password.

I would probablty rewrite the code to return a boolean, or even a numeric value

Code:
'within the form
    Me.DeleteBtn.Visible = AdminUser  'result of the function below

Code:
function AdminUser() as boolean
adminuser = "code to determine the appropriate access level"
End If

Thanks for the reply.

I've never written a function before so I'm not sure where to start for this.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:34
Joined
Aug 30, 2003
Messages
36,123
UserAccess would need to be a function in a module that checked whether the user was an admin or not.

You would also need a user to log in on starting the app, so that you could detewrime his level of access based in his name and password.

Well, it doesn't need to be a function, it could be a variable previously set with the user's access level. Also, you don't need a user to log in, you could use the Windows login if appropriate.
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
Well, it doesn't need to be a function, it could be a variable previously set with the user's access level. Also, you don't need a user to log in, you could use the Windows login if appropriate.

I already use a login form and a tblemployees on the backend of that form. I also have tblPermissions which holds the levels of access available. This has two fields, Level (numeric) and User (text)

As a relative beginner I'm uncertain how to code this to hide the button for non admin users (Level 1) so any help is appreciated.
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
I'm really stuck with this so if anyone could please help to save my sanity.

I've been looking for examples and watching YouTube vids on this all day and I'm still scratching my head.

I tried creating TempVar and dlookup but I can't get them to work.

If someone could please help. The structure of the table is as in my OP.
 

Micron

AWF VIP
Local time
Today, 05:34
Joined
Oct 20, 2018
Messages
3,478
The issue I see at this point is that you haven't revealed if more than one user can be logged in at the same time, or how to determine who's opening the form that you want to hide the button from. You say you have a table of logged in users, but how do we know Smith is opening the form and not Brown?

If the login form stays open all the time, that could be used. Or there is the use of TempVars. Or as already stated, you don't need login if you just get the Windows user login ID (unless everyone shares one pc and you need them to log in and out of the db only). So to reiterate, what have you got that distinguishes who is opening the form? Apologies if I missed that.
 

xyba

Registered User.
Local time
Today, 10:34
Joined
Jan 28, 2016
Messages
189
The issue I see at this point is that you haven't revealed if more than one user can be logged in at the same time, or how to determine who's opening the form that you want to hide the button from. You say you have a table of logged in users, but how do we know Smith is opening the form and not Brown?

If the login form stays open all the time, that could be used. Or there is the use of TempVars. Or as already stated, you don't need login if you just get the Windows user login ID (unless everyone shares one pc and you need them to log in and out of the db only). So to reiterate, what have you got that distinguishes who is opening the form? Apologies if I missed that.

I've possibly not explained too well.

The table (tblEmployees) stores the user name, password and level of permissions (admin or user). The login form is simply used to input username and password checking that the password matches that recorded in tblEmployees. It doesn't store login history.

I need to identify the user access permission level to be able to hide specific controls on forms, for example a delete button.

I just don't know what code to use to identify the permissions level for the user that has logged in using the db login form.

Only one user will be logged in at a time as number of users it small. I need to use the db login and not windows.
 

Micron

AWF VIP
Local time
Today, 05:34
Joined
Oct 20, 2018
Messages
3,478
The table (tblEmployees) stores the user name, password and level of permissions (admin or user)
I realize that, but what I'm saying is that you need a way to identify who that currently is. If there is no current/temporary login record or history you cannot get the last/max of any user data.

My suggestion would be check out the use of TempVars as already noted, or when password has been evaluated, open a hidden form that holds the user data. When you try to open any subsequent form, your open (or sometimes load event) code can determine what level they are and hide/show controls as needed.

There are better ways to do this, but IMHO the form is probably the easiest method for you and seems like it would meet your requirements. The next easiest method might be to at least have a table of logged in users but you still need a way to know which record to look at to get their level. The most difficult is probably a custom user object (my fave) with all the required user attributes you need.
 

gswrco

New member
Local time
Today, 02:34
Joined
Oct 12, 2019
Messages
1
I do have a similar weired condition. I have a login form which look up the permission_ID and transfer it to hidden text control on the main start form. I have put the following code in the on load event of the start form.

Private Sub Form_Load()

If Me.Per.Value = 1 Then
Me.Command17.Visible = True
Me.Command15.Visible = True
Me.Command0.Visible = True
Else
Me.Command17.Visible = False
Me.Command15.Visible = False
Me.Command0.Visible = False
End If
End sub

where Per is the hidden text box receiving Permission_ID from login form. The strange matter that on opening the start form nothing happen but if I switch to form design and back to form view it works O.K.

Any help on this situation ? I am working on Access 2003
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 10:34
Joined
Jan 14, 2017
Messages
18,209
Hi gswrco
Firstly welcome to AWF.

Try saving the form in design view to set the controls to the desired state.
Or modify your code as follows
Code:
Private Sub Form_Load()

 'set defaults   
  Me.Command17.Visible = False
  Me.Command15.Visible = False
  Me.Command0.Visible = False

    If Me.Per.Value = 1 Then
        Me.Command17.Visible = True
        Me.Command15.Visible = True
        Me.Command0.Visible = True
    End If
End Sub

I would strongly recommend you use meaningful names for your command buttons such as cmdPrint, cmdClose.
Also its better to start your own thread rather than 'hijack' an existing one.
 

Megaduck

Member
Local time
Today, 05:34
Joined
Apr 24, 2020
Messages
30
I've used this with success:

Code:
Private Sub Form_Load()

Dim UserLevel As String

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

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

It looks in a table "Employee List" for their "Role" where their "Employee Name" is equal to a tempvar set from the login screen.

You might be able to use this, modifying it to your needs.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:34
Joined
Feb 28, 2001
Messages
27,131
You've gotten several suggestions. The biggest issue you had with your original code is that during "Form_Open" the controls aren't available yet. You need to use the Form_Load event. That's where I did what you were trying to do.

But what I did was I had a "switchboard" style of form as my "Opening Form." In that switchboard's Form_Open routine, I could see who it was with the Environ("Username") function. Some problems can exist with this, but in my Navy environment, people couldn't spoof their identities because the terminals were too tightly locked down. They had logged in via a domain, so that username was what I used for a lookup in the user table to find their role. The switchboard's Form_Open couldn't see the controls, but it COULD see public variables in a general module set aside for this purpose. So I loaded the username and the lookup of the user role. Then each form's Form_Load could look at the public variable showing their role to decide what to show and what not to show.
 

Users who are viewing this thread

Top Bottom