Button visible only for Admin and not to users

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashok srmuniv
    New Member
    • Apr 2012
    • 9

    Button visible only for Admin and not to users

    Hi , I have a login page(form1),whe n we enter the login page it takes to a new form(form3) to view their informations.An d 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.
  • kstevens
    New Member
    • Mar 2009
    • 74

    #2
    Code:
    if username="Admin" then
    me.adduserbtn.visible = true
    else
    me.adduserbtn.visible = false
    end if/
    Last edited by Niheel; Apr 3 '12, 11:49 PM.

    Comment

    • ashok srmuniv
      New Member
      • Apr 2012
      • 9

      #3
      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.

      Comment

      • kstevens
        New Member
        • Mar 2009
        • 74

        #4
        post the way you wrote the code, so i can see more specifics...and perhaps a couple user IDs and usernames...

        Comment

        • ashok srmuniv
          New Member
          • Apr 2012
          • 9

          #5
          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.
          Code:
          Private Sub Login_button_Click()
          
          Dim UN, PW As Boolean
          Dim Name, Lname, Fname As String
              If IsNull(Me.User_name) Or Me.User_name = "" Then
                MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
                  Me.User_name.SetFocus
                  Exit Sub
              Else
              UN = True
              End If
                If IsNull(Me.Password) Or Me.Password = "" Then
                MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
                  Me.Password.SetFocus
                  Exit Sub
                  Else
                  PW = True
                   End If
            If UN = True And PW = True Then
           If Me.Password.Value = DLookup("[Password]", "Info", "[User ID]='" & Me.User_name & "'") Then
              DoCmd.Close acForm, Me.Name
              DoCmd.OpenForm "Info_form"
          Else
             MsgBox "Incorrect Password"
             End If
             End If
          End Sub
          Last edited by NeoPa; Apr 4 '12, 03:02 AM. Reason: Added mandatory [CODE] tags for you

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32668

            #6
            Try (after line #22) :
            Code:
            Forms("Info_Form").AddNewUser.Visible = (Me.User_Name = "Admin")
            NB. Your Dim code on line #3 is equivalent to :
            Code:
            Dim UN As Varian, PW As Boolean
            rather than :
            Code:
            Dim UN As Boolean, PW As Boolean
            Each variable needs to be set explicitly. The same problem with line #4 of course.

            Comment

            • Mihail
              Contributor
              • Apr 2011
              • 759

              #7
              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:
              Code:
              Private Sub Login_button_Click()
                  If IsNull(Me.User_Name) Or Me.User_Name = "" Then
                      MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
                      Me.User_Name.SetFocus
              Exit Sub
                  End If
                  'the User name has been imputted
                  If IsNull(Me.Password) Or Me.Password = "" Then
                      MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
                      Me.Password.SetFocus
              Exit Sub
                  End If
                  'the Password has been inputted
                  If Me.Password.Value = DLookup("[Password]", "Info", "[User ID]='" & Me.User_Name & "'") Then
                      'Open "Info_form" and pass the User_Name in OpenArgs parameter
                      DoCmd.OpenForm "Info_form", , , , , , Me.User_Name
                      DoCmd.Close acForm, Me.Name
                  Else
                      MsgBox "Incorrect Password"
                  End If
              End Sub

              In Info_form form paste the following code: (of course you must change "AdminName" with the real name)
              Code:
              Private Sub Form_Open(Cancel As Integer)
                  Me.AddNewUser.Visible = (Me.OpenArgs = "AdminName")
              End Sub

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32668

                #8
                Originally posted by Mihail
                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.

                Comment

                • Mihail
                  Contributor
                  • Apr 2011
                  • 759

                  #9
                  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 !.

                  Comment

                  • ashok srmuniv
                    New Member
                    • Apr 2012
                    • 9

                    #10
                    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 ??

                    Comment

                    • Mihail
                      Contributor
                      • Apr 2011
                      • 759

                      #11
                      As NeoPa will say... we are happy to help you but new question = new thread.

                      Comment

                      • ashok srmuniv
                        New Member
                        • Apr 2012
                        • 9

                        #12
                        Okay :) Will start that rite now

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32668

                          #13
                          Originally posted by Mihail
                          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.

                          Comment

                          Working...