Trying to create login form which will open specific form depending on user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kondratiev
    New Member
    • Jan 2015
    • 4

    Trying to create login form which will open specific form depending on user

    Hello,

    I am trying to create a user login form for an Access database. It will have three users and will open a different form depending on which user logs in to the database. So far I have only been able to create code which opens a single form no matter which user logs in. The code below shows the code I have made to accomplish this:

    Code:
    Public Sub Login()
            
    On Error GoTo ErrorHandler:
    
        If IsNull([cboUser]) = True Then 'Checks username entered
            MsgBox "Username is required"
            
        ElseIf IsNull([txtPassword]) = True Then 'Checks validity of password entered
            MsgBox "Password is required"
            
        Else
        
            'Compare password entered w/ password saved in table of users
            If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
                strUser = Me.cboUser.Value 'Set the value of strUser declared as Global Variable
                strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 
                DoCmd.Close acForm, "frmLogin", acSaveNo
                MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
                DoCmd.OpenForm "frmPersonnelOfficer", acNormal, "", "", , acNormal
                
            Else
                MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
                intLogAttempt = intLogAttempt + 1
                txtPassword.SetFocus
        
            End If
        
        End If
        
        'If user enters PW incorrectly three times
        If intLogAttempt = 3 Then
            MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
            "Application will exit.", vbCritical, "Restricted Access!"
            Application.Quit
        End If
        
    ErrorHandler:
    
    End Sub



    I would like to know what I would need to add to this so that a specific form would open for each user depending on the role which the user is assigned. For example, and admin user would open an admin form upon entering their password while a nurse user would see a nurse form having successfully entered their password.

    Any help is appreciated.
    Last edited by Rabbit; Jan 13 '15, 12:43 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    You can do something like the following for line 19:
    Code:
        Select Case strRole
            Case "Nurse"
                sForm = "frmPersonnelOfficer"
            Case "Doctor"
                sForm = "frmPersonnelOfficer2"
            Case Else
        End Select
        DoCmd.OpenForm sForm, acNormal, "", "", , acNormal

    Comment

    • Kondratiev
      New Member
      • Jan 2015
      • 4

      #3
      Thanks for posting that. It's helped me a lot.

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3653

        #4
        Kondratiev,

        You may also want to look at this article, as it may provide additional options for you:

        MS Access User Permissions

        Comment

        Working...