How do I make a button availible for only one user (1 Viewer)

ScoobsR

New member
Local time
Today, 07:41
Joined
Nov 20, 2015
Messages
7
Ok this is the first thread I have created, so I apologise if I'm in the wrong area or anything like that 😕

Ok I'm also pretty new to coding in access, but here is my situation.....

I have a relational database with multiple tables and forms, in order to get to the forms I have a login screen which is a form for a login table named : tblLogin ( the coding for the login part has already been done)

There is another table called: tblAdmin which has a single foreign key look up field relating to the username field of tblLogin - this table only has one single record which obv is a user from the login table.

Now once a user is through login screen and into the main menu, naturally I want to have navigation buttons on each form, the tricky bit is, I want to have a delete record button on each of these forms, but the delete record button should only be visible if the user held in the tblAdmin is logged in

Can anyone help me with how to code this, and where I would attach the code? (To each button or to each form)
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:41
Joined
Aug 30, 2003
Messages
36,134
I'd use the open event of the form to hide however many buttons are on that form. You can test with DCount():

Code:
If DCount(...) > 0 Then
  'user is an admin
Else
  'user is not
End If

I would probably have had an "admin" field in the user table rather than a separate admin table.
 

ScoobsR

New member
Local time
Today, 07:41
Joined
Nov 20, 2015
Messages
7
Hi, thanks for the reply 👍🏻

Sorry for the noob question, but what is DCount() ?

And I want to make the delete button invisible to everyone apart from one user, but I want it to be coded in a way that if that user changes I.e a different user is entered into the tblAdmin - the code does not have to be altered.
 

Cronk

Registered User.
Local time
Tomorrow, 00:41
Joined
Jul 4, 2013
Messages
2,774
"Noob" questions can mostly be answered by a simple Google search. Try searching "ms access dcount"

If you put in the user logon name as the criteria for DCount with your tblAdmin as the domain, the result of the Dcount function is more than zero if the logon name appears in the table.

To hide a button, use the control visible property and set it to false in the form open event (as Paul suggested)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:41
Joined
May 7, 2009
Messages
19,246
also can be done on your form's Load event:

Private Sub Form_Load()
Me.cmdDelete.Visible = (NZ(Dcount("*","tblAdmin", "useNameField = '" & userName & "'"), 0) <> 0)
End Sub
 

ScoobsR

New member
Local time
Today, 07:41
Joined
Nov 20, 2015
Messages
7
Hi guys, thanks alot for your help with this, i ahve another question or 2 tho....


I cant seem to get to the "Private sub Form_Load()" part to put the code in :banghead:
 

jdraw

Super Moderator
Staff member
Local time
Today, 10:41
Joined
Jan 23, 2006
Messages
15,396
ScoobsR,

There are several great tutorials on Youtube and the internet generally. You can learn a lot quickly if you use google and look for examples.

Excellent youtube MS Access tutorials by
599CD
Steve Bishop
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:41
Joined
May 7, 2009
Messages
19,246
you put it inside each form, that you have delete button, substituting the correct name for the command button for Me.cmdDelete
 

Users who are viewing this thread

Top Bottom