By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,775 Members | 2,658 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,775 IT Pros & Developers. It's quick & easy.

Button visible only for Admin and not to users

P: 9
Hi , I have a login page(form1),when we enter the login page it takes to a new form(form3) to view their informations.And i have a button to add new users,Who ever enters the form can access the button called "Add New User".
but i want to make it visible only when the "ADMIN" log in. Please help me I am new to Access.
Apr 3 '12 #1
Share this Question
Share on Google+
12 Replies


P: 74
Expand|Select|Wrap|Line Numbers
  1. if username="Admin" then
  2. me.adduserbtn.visible = true
  3. else
  4. me.adduserbtn.visible = false
  5. end if/
  6.  
Apr 3 '12 #2

P: 9
Hi Stev,
Thanks for your help. I tried this code and it is just hiding the button for all the users, I mean even for the ADMIN. Please help.
let me give you the full details. "User_name" comes from the table1 ie from "User ID". So on loading the form , if it is ADMIN then it should have access. plz help me.
Apr 3 '12 #3

P: 74
post the way you wrote the code, so i can see more specifics...and perhaps a couple user IDs and usernames...
Apr 3 '12 #4

P: 9
Hi Steve,
This is the Code which i have rite now.
So when i login as ADMIN , the "addnewuser" button in the "infoform" should should show up and it should get hidden for other users.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Login_button_Click()
  2.  
  3. Dim UN, PW As Boolean
  4. Dim Name, Lname, Fname As String
  5.     If IsNull(Me.User_name) Or Me.User_name = "" Then
  6.       MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
  7.         Me.User_name.SetFocus
  8.         Exit Sub
  9.     Else
  10.     UN = True
  11.     End If
  12.       If IsNull(Me.Password) Or Me.Password = "" Then
  13.       MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
  14.         Me.Password.SetFocus
  15.         Exit Sub
  16.         Else
  17.         PW = True
  18.          End If
  19.   If UN = True And PW = True Then
  20.  If Me.Password.Value = DLookup("[Password]", "Info", "[User ID]='" & Me.User_name & "'") Then
  21.     DoCmd.Close acForm, Me.Name
  22.     DoCmd.OpenForm "Info_form"
  23. Else
  24.    MsgBox "Incorrect Password"
  25.    End If
  26.    End If
  27. End Sub
Apr 3 '12 #5

NeoPa
Expert Mod 15k+
P: 31,417
Try (after line #22) :
Expand|Select|Wrap|Line Numbers
  1. Forms("Info_Form").AddNewUser.Visible = (Me.User_Name = "Admin")
NB. Your Dim code on line #3 is equivalent to :
Expand|Select|Wrap|Line Numbers
  1. Dim UN As Varian, PW As Boolean
rather than :
Expand|Select|Wrap|Line Numbers
  1. Dim UN As Boolean, PW As Boolean
Each variable needs to be set explicitly. The same problem with line #4 of course.
Apr 4 '12 #6

100+
P: 759
I think that the button visibility should be handle in the Info_form form using _Open event.
I have cleaned a little bit your code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Login_button_Click()
  2.     If IsNull(Me.User_Name) Or Me.User_Name = "" Then
  3.         MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
  4.         Me.User_Name.SetFocus
  5. Exit Sub
  6.     End If
  7.     'the User name has been imputted
  8.     If IsNull(Me.Password) Or Me.Password = "" Then
  9.         MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
  10.         Me.Password.SetFocus
  11. Exit Sub
  12.     End If
  13.     'the Password has been inputted
  14.     If Me.Password.Value = DLookup("[Password]", "Info", "[User ID]='" & Me.User_Name & "'") Then
  15.         'Open "Info_form" and pass the User_Name in OpenArgs parameter
  16.         DoCmd.OpenForm "Info_form", , , , , , Me.User_Name
  17.         DoCmd.Close acForm, Me.Name
  18.     Else
  19.         MsgBox "Incorrect Password"
  20.     End If
  21. End Sub
  22.  

In Info_form form paste the following code: (of course you must change "AdminName" with the real name)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.AddNewUser.Visible = (Me.OpenArgs = "AdminName")
  3. End Sub
Apr 4 '12 #7

NeoPa
Expert Mod 15k+
P: 31,417
Mihail:
I think that the button visibility should be handle in the Info_form form using _Open event.
Why?

It's certainly a way to handle it, but I don't see why it would be preferable necessarily.
Apr 4 '12 #8

100+
P: 759
Because a form can be opened from many places.
This way, the form itself handle their controls. This avoid to handle this situation in every place (other forms or codes) from where this form can be opened.
And, NeoPa, I say "I think" not "must".
Another reason is to reduce the risk. For exemple I am not sure that your solution (Try (after line #22)... - post #6) can work. Because the line #21.
I repeat: I am not sure. Maybe work, maybe not.
It is why I have inverted line #21 with line #22 (#16, #17 in my post).
Lastly, "I think" that because this approach is better fit to my style in programming :)

Cheers !.
Apr 4 '12 #9

P: 9
Hi NeoPa and Mihail
Thanks a lot for your help, both the codes are working and when it works without using boolean and varian, why do we need it ?? And also i need one more help.

When a user login , it should show only the user's data, so when it login to the info form it should show their corresponding form.. how to make that ??
Apr 4 '12 #10

100+
P: 759
As NeoPa will say... we are happy to help you but new question = new thread.
Apr 4 '12 #11

P: 9
Okay :) Will start that rite now
Apr 4 '12 #12

NeoPa
Expert Mod 15k+
P: 31,417
Mihail:
For example I am not sure that your solution (Try (after line #22)... - post #6) can work. Because the line #21.
Good thinking, but the form cannot close while the form's module code is still running. It's a bit of a silly line to have in the code at that place, but one can't always comment on every problem with members' code as there can be so many.
Apr 4 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.