Before creating a Login Form, you need to set up a user table that can verify the login ID and password on the Login Form. The step of creating Login Form can be followed below:
Create a table tblSecurity with a SecurityID and SecurityLevel field and add admin for SecurityID =1 and user for SecurityID =2
Create a table tblUser with a UserName, UserLogin, UserSecurity and UserPassword fields. Remember to add an input mask on the password field so that passwords are not blatantly visible in the table.
I choose to display the type of user rather than just the raw number of the User’s security level by using the Lookup options in the design view. This way the UserType field is a number corresponding to the User’s security level because it refers to the SecurityID in the tblSecurity but displays the user’s UserType in text (admin, user etc.). You can create the UserType field from the Lookup Wizard on the dropdown of Data Type column.
Create a Login Form from the Dialog form design. Then customize the form to your liking.
Create two text boxes in the Login Form as txtUserName with label Login Name and txtPassword with label Password
Under the “On Click Event” of the Cancel button, add the Embedded Macro with a QuitAccess command to exit the program or Access application
Create a Navigation and Admin Form area to reroute your users to the appropriate area of your database application.
Under On Click Event of the OK button, add the VBA code below under the Event Procedure
Option Compare Database Private Sub Command1_Click() Dim User As String Dim UserLevel As Integer Dim TempPass As String Dim ID As Integer Dim UserName As String Dim TempID As String If IsNull(Me.txtUserName) Then MsgBox "Please enter UserName", vbInformation, "Username required" Me.txtUserName.SetFocus ElseIf IsNull(Me.txtPassword) Then MsgBox "Please enter Password", vbInformation, "Password required" Me.txtPassword.SetFocus Else If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then MsgBox "Invalid Username or Password!" Else TempID = Me.txtUserName.Value UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") DoCmd.Close If (TempPass = "password") Then MsgBox "Please change Password", vbInformation, "New password required" DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin Else 'open different form according to user level If UserLevel = 1 Then ' for admin DoCmd.OpenForm "Admin Form" Else DoCmd.OpenForm "Navigation Form" End If End If End If End If End Sub Private Sub Form_Load() Me.txtUserName.SetFocus End Sub
Set the Login Form to display immediately when the Access Database is opened. Go to File->Options and select the Current Database option from the list. Then set the Login Form as the display form.
It should be noted that while this login procedure is helpful to have permissions and different roles withing your Access database and may be needed for our Remote Desktop Hosting – it is not necessary for any hosted Access Web App or Sharepoint Application since you can use the Sharepoint user roles and login procedures rather than handling it within the Access Database itself.
is there any video tutorial of this?
This works for the most part, but there is an unexplained form “frmUserinfo” that needs to be created for it to work with a new user that has “password” for their default. Please add the details for this form.
change userType into UserSecurity when you get an error
WORKS LIKE A CHARM!!! THANKS
How do you link to a new form after login? Complete noob to this.
Can you explain the fix in more detail please?
Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help 🙁
Agreed. This is extremely helpful and I’m curious about setting up the “frmUserinfo” and how it correlates to changing to a new password from their default, too. Any help here would be appreciated.
Error coming saying method or data member not found
Can you post a link to your access file with the code?
Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help 🙁
Login form worked properly. How to link my db with login form.
This is amazing – would have taken me so long to work out alone! With just a couple of changes I could adapt it to my already existing DB. Thank you
Do you actually have a control that has the name “txtUserName” ? Also you may want to actually ensure you have interactively specified the form_load() routine from the correct form you are using. You can do this by going to the properties of your form and selecting the Event tab on the property sheet. Select on load. Then go to code builder (not macro builder etc.) (note this applies to Access 2016.)
I think all you have to do is specify the form you want to be directed to. See code: If UserLevel = 1 Then ‘ for admin
DoCmd.OpenForm “Admin Form”
Else
DoCmd.OpenForm “Navigation Form”
End If
So for your application, specify the form you want to open replace “admin form” with the name of your form or “Navigation Form” with the appropriate form etc.
Unless I misunderstood your question.
Do you really need it? meaning just remove the logic that checks the password….if you need that password check, then yes create the form that will store the new password. In the new form force the user to change the password – follow the same logic and then direct them to the form they need to reach….
How do a track when users login to the database? I have tried multiple different vba coding configurations, and they do not work. If you have any insight that would be great.
Yes please do. I have tried with no luck.
I’m getting same error on the line the is Me.txtUserName.SetFocus under Private Sub Form_Load () and the Error coming saying method or data member not found. Whomever replied it works, can you post the VBA code in whole? I did above step by step in new database before adding any company data, and no joy.
same here..Error coming saying method or data member not found.. 🙁
I ve implement the above example and it works perfect. I have 2 questions. 1.Does it work on the web? 2. How do i setup specific users 2 view only their records ? (according 2 username and password they enter)
I typed the vba code but after ok nothing is happened. File error 2471 can help any please
What do you want to do? To open another form after login?
If that is the case, then
DoCmd.OpenForm “Admin Form”
Else
DoCmd.OpenForm “Navigation Form”
End If
these two lines open the forms depending on user level
Check the spelling of the naming of your textboxes
The Password validation is not case sensitive. Any thoughts on fixing it?
you have to create the forms. The logic is already there to trigger forms named “Admin Form” and “Navigation Form”.
Macro for “QuitAccess” appears to have been changed to “Exit”.
This works like magic, for the Me.txtuser error, you have to customize the right cell name so the code will work. Thank you!
I want link download this sample
its working but you can used the password of other users to log in
I am getting an error “Ambiguous Name Detected: Command1_Click when opening the form.
its not working for my access 2007
“Create a Navigation and Admin Form area to reroute your users to the appropriate area of your database application”. what does this part of the guide mean?
The code works great. My question is how do you get the login screen to open without any login info pre-filled? Right now it opens right up with the admin login info prefilled. Pretty much defeats the purpose.
I have noticed a problem that while it does require a password and username, they do no need to match.. I have noticed that as long as the password is in the table, it does not have to be the one associated with that particular user, it will still provide access. Any ideas why this is?
thank you so much it works ,, how to get the username in other form ???
What is the purpose of the UserLogin field?
Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help 🙁
thanks for this.. works great!
how would I retrieve that username to use on a form to display the current user ?
Error coming saying method or data member not found
This is a very useful form and good explanation .
But I have the same need as db: I need to use the username (UserName) on the header of the following forms, as well as have access to the user level (UserLevel) for further flow and access control. I think that these variables cannot be used outside of this macro because they are defined as Private. How can I have them available outside of this macro?
Thank you.
your the best
HI
Could you please explain frmUserinfo form?
Great work!!
Do you have an example of drag and drop files from explorer to listbox
Thank’s in advance
I have the same question
I am getting an error “Ambiguous Name Detected: Command10_Click when opening the form.
same here! have you solved the problem?
Error saying “method or data member not found” then highlight “Private Sub Form_Load()
Me.txtUserName.SetFocus”… can anyone help me on this please? This is the vba code…
+++++++++++++++++++++++++++++++++++++++++++++
Option Compare Database
Private Sub Command1_Click()
Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim UserName As String
Dim TempID As String
If IsNull(Me.txtUserName) Then
MsgBox “Por favor ingrese Nombre de Usuario”, vbInformation, “Nombre de Usuario requerido”
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox “Por favor ingrese su clave”, vbInformation, “Clave requerida”
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup(“UserLogin”, “tblUser”, “UserLogin = ‘” & Me.txtUserName.Value & “‘ And UserPassword = ‘” & Me.txtPassword.Value & “‘”))) Then
MsgBox “Nombre de usuario o clave inválida!”
Else
TempID = Me.txtUserName.Value
UserName = DLookup(“[UserName]”, “tblUser”, “[UserLogin] = ‘” & Me.txtUserName.Value & “‘”)
UserLevel = DLookup(“[UserType]”, “tblUser”, “[UserLogin] = ‘” & Me.txtUserName.Value & “‘”)
TempPass = DLookup(“[UserPassword]”, “tblUser”, “[UserLogin] = ‘” & Me.txtUserName.Value & “‘”)
UserLogin = DLookup(“[UserLogin]”, “tblUser”, “[UserLogin] = ‘” & Me.txtUserName.Value & “‘”)
DoCmd.Close
If (TempPass = “password”) Then
MsgBox “Por favor cambie su clave”, vbInformation, “Clave nueva requerida”
DoCmd.OpenForm “frmUserinfo”, , , “[UserLogin] = ” & UserLogin
Else
‘open different form according to user level
If UserLevel = 1 Then ‘ for admin
DoCmd.OpenForm “Formulario de navegación”
Else
DoCmd.OpenForm “Formulario de navegación”
End If
End If
End If
End If
End Sub
Private Sub Form_Load()
Me.txtUserName.SetFocus
End Sub
+++++++++++++++++++++++++++++++++++++++++++++
To open the form frmUserinfo the filter in above script is not working in Access 2016/Office 365:
DoCmd.OpenForm “frmUserinfo”, , , “[UserLogin] = ” & UserLogin
Replace it with:
DoCmd.OpenForm “frmUserinfo”, , , “[UserLogin] = ‘” & UserLogin & “‘”
It will works like a charm. frmUserinfo will open with the correct user filtered
Working perfectly…
thanks