Advanced Login System with Admin Privilege?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhuns
    New Member
    • Jun 2010
    • 8

    Advanced Login System with Admin Privilege?

    I have a database with a table called activeusers and 3 fields. "UserID" "Password" and "Admin". Admin is a yes/no field. I would like to use this field to enable certain buttons on the switchboard screen. how can I use vba to check wether the Admin feild is checked or not?
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1290

    #2
    In the switchboard program, you will want to either set the .visible property or the .enabled property of the buttons you want to turn on or off.

    The code will be
    Code:
    if me!nameofadmincheckbox = true then
       ' do true stuff
    else
        ' do false stuff
    endif
    You might want to put this code in the OnLoad event of the form and also on the click event of whatever buttons moves to another user ID. Or maybe you want to put it in after the password is checked. Probably there is best, after they login you will turn the buttons on or off.

    Jim

    Comment

    • rhuns
      New Member
      • Jun 2010
      • 8

      #3
      Originally posted by jimatqsi
      In the switchboard program, you will want to either set the .visible property or the .enabled property of the buttons you want to turn on or off.

      The code will be
      Code:
      if me!nameofadmincheckbox = true then
         ' do true stuff
      else
          ' do false stuff
      endif
      You might want to put this code in the OnLoad event of the form and also on the click event of whatever buttons moves to another user ID. Or maybe you want to put it in after the password is checked. Probably there is best, after they login you will turn the buttons on or off.

      Jim
      I apologize I may not have been as clear as I needed to be. There are two forms I have: one a login and one a menu. I want certain buttons enabled to certain users and other buttons not enabled to certain users. Is there a way I set the current record through the login screen? Here is the code I have so far:

      Code:
      Private Sub Command8_Click()
      Username.SetFocus
      If Username = DLookup("UserID", "ActiveUsers", "[password] = '" & Password & "'") Then
          DoCmd.Close
          DoCmd.OpenForm "MainMenu"
      Else
          MsgBox "Inncorrect Username or Password"
      End If
      End Sub

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).

        Perhaps, killing two birds with one stone, the following could help :
        Code:
        Private Sub Command8_Click()
            Dim strFilter As String, strAdmin As String
        
            Call Username.SetFocus
            strFilter = "(([UserID]='%U') AND ([Password]='%P'))"
            strFilter = Replace(strFilter, "%U", Me.UserName
            strFilter = Replace(strFilter, "%P", Me.Password
            strAdmin = Nz(DLookup("CStr([Admin])", _
                                  "[ActiveUsers]", _
                                  strFilter), "Fail")
            If strAdmin = "Fail" Then
                Call MsgBox("Incorrect Username or Password")
            Else
                Call DoCmd.OpenForm(FormName:="MainMenu" _
                                   ,OpenArgs:=strAdmin)
                Call DoCmd.Close
            End If
        End Sub

        Comment

        • rhuns
          New Member
          • Jun 2010
          • 8

          #5
          Originally posted by NeoPa
          You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).

          Perhaps, killing two birds with one stone, the following could help :
          Code:
          Private Sub Command8_Click()
              Dim strFilter As String, strAdmin As String
          
              Call Username.SetFocus
              strFilter = "(([UserID]='%U') AND ([Password]='%P'))"
              strFilter = Replace(strFilter, "%U", Me.UserName
              strFilter = Replace(strFilter, "%P", Me.Password
              strAdmin = Nz(DLookup("CStr([Admin])", _
                                    "[ActiveUsers]", _
                                    strFilter), "Fail")
              If strAdmin = "Fail" Then
                  Call MsgBox("Incorrect Username or Password")
              Else
                  Call DoCmd.OpenForm(FormName:="MainMenu" _
                                     ,Openargs:=strAdmin)
                  Call DoCmd.Close
              End If
          End Sub
          Neopa,

          I tried your code and it works!

          However, how would make the distinction of and Admin vs. a regular user?

          I.E.:

          I want certain buttons, like addnewbtn, or, updatebtn, only enabled to Administrators, not regular users. How could this be accomplished?

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            Originally posted by NeoPa
            NeoPa: You need to reconsider your code. It is not reliable as the [Password] is not defined as unique (I hope).

            Perhaps, killing two birds with one stone, the following could help :
            Code:
            Private Sub Command8_Click()
                Dim strFilter As String, strAdmin As String
            
                Call Username.SetFocus
                strFilter = "(([UserID]='%U') AND ([Password]='%P'))"
                strFilter = Replace(strFilter, "%U", Me.UserName
                strFilter = Replace(strFilter, "%P", Me.Password
                strAdmin = Nz(DLookup("CStr([Admin])", _
                                      "[ActiveUsers]", _
                                      strFilter), "Fail")
                If strAdmin = "Fail" Then
                    Call MsgBox("Incorrect Username or Password")
                Else
                    Call DoCmd.OpenForm(FormName:="MainMenu" _
                                       ,OpenArgs:=strAdmin)
                    Call DoCmd.Close
                End If
            End Sub
            If you look at line #15 in the code you'll see this is already passed to the form.

            To use this you need to set a variable to this value as soon as the form is opened (as it is lost very soon afterwards). If nothing is passed then OpenArgs is Null.

            The code in your called form should be something like :
            Code:
            Private strFrom As String
            
            Private Sub Form_Open(Cancel As Integer)
                strFrom = Nz(OpenArgs, "")
            End Sub
            Last edited by NeoPa; Jul 7 '10, 09:48 AM. Reason: Quoted to include in Best Answer

            Comment

            • rhuns
              New Member
              • Jun 2010
              • 8

              #7
              NeoPa,

              Your code worked perfectly! Thanks so much! I've been stuck for over a week trying to debug this one. Thanks again!

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32634

                #8
                I'm pleased to have been able to help :)

                Comment

                Working...