Вы находитесь на странице: 1из 13

4/11/2019 How to Create a Login Form for MS Access

Live Chat Sales:(302) 246-5567 Support Try It!

Solutions Remote Desktop SQL SharePoint Pricing Free Trials Company

Data Center & Security


How to Create a Login Form for MS Access

Posted on November 7, 2016 by admin

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

Our data centers are world-class


facilities with carefully selected
locations that provide a redundancy
and top of the line security.

Do you have questions or


need help?

If you need help selecting the right hosting


plan or have a Sales question, please chat
with a Access Hosting Sales Operator

Sales: (302) 468-7475

Get your questions answered by watching


some of our Video Tutorials. Get help 24/7
from our Helpdesk and Forums

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.
Latest Blog Posts

Access Web App Hosting from


Office 365 is here
How to migrate your Office 365
Access Web App to Access
Hosting
Microsoft Azure RemoteApp is no
more, but don’t worry we can
help!
5 Hidden Costs of Hosting Your
Database On-Premise
The 5 Pitfalls of Windows Azure
Hosting

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.

accesshosting.com/create-login-form-ms-access 1/13
4/11/2019 How to Create a Login Form for MS Access

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

accesshosting.com/create-login-form-ms-access 2/13
4/11/2019 How to Create a Login Form for MS Access

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 User
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

accesshosting.com/create-login-form-ms-access 3/13
4/11/2019 How to Create a Login Form for MS Access
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.

Share this:

  

Related

Getting Started with an How to create an MS Access The Top 10 Reasons to Split
Access Wep App in 2013 Web App from within an Access database
Sharepoint Sharepoint Enteprise February 25, 2015
July 19, 2016 June 11, 2015 In "Access 2003"
In "Access 2013" In "Access 2013"

This entry was posted in Access 2016, Remote Desktop Hosting, Tutorial and tagged Access 2016, Remote
Desktop, VBA code sample. Bookmark the permalink.

← Previous Post Next Post →

51 Responses to How to Create a Login Form for MS Access

accesshosting.com/create-login-form-ms-access 4/13
4/11/2019 How to Create a Login Form for MS Access

Liakat Biswas says:

January 9, 2017 at 1:50 pm

is there any video tutorial of this?

Reply

slelgan says:

January 31, 2017 at 10:06 am

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.

Reply

faza says:

February 9, 2017 at 10:56 pm

change userType into UserSecurity when you get an error

Reply

Manny BrobbeyManny says:

February 22, 2017 at 9:22 pm

WORKS LIKE A CHARM!!! THANKS

Reply

electrifekt says:

March 19, 2017 at 12:49 pm

How do you link to a new form after login? Complete noob to this.

Reply

Dave says:

March 24, 2017 at 2:15 pm

Can you explain the fix in more detail please?

Reply

S says:

March 27, 2017 at 12:30 pm

Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help

Reply

lynich says:

March 30, 2017 at 11:19 am

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.

Reply

accesshosting.com/create-login-form-ms-access 5/13
4/11/2019 How to Create a Login Form for MS Access

Vsm says:

April 2, 2017 at 10:56 am

Error coming saying method or data member not found

Reply

Tlhogi says:

April 5, 2017 at 6:23 am

Can you post a link to your access file with the code?

Reply

Pdiddy says:

May 11, 2017 at 5:35 am

Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help

Reply

Manidhan Singha says:

May 12, 2017 at 8:42 am

Login form worked properly. How to link my db with login form.

Reply

Melanie says:

June 7, 2017 at 11:51 pm

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

Reply

idmrentals says:

June 8, 2017 at 12:32 pm

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.)

Reply

idmrentals says:

June 8, 2017 at 12:33 pm

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.

Reply

accesshosting.com/create-login-form-ms-access 6/13
4/11/2019 How to Create a Login Form for MS Access

idmrentals says:

June 8, 2017 at 12:37 pm

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….

Reply

S says:

June 8, 2017 at 2:04 pm

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.

Reply

S says:

June 14, 2017 at 4:29 pm

Yes please do. I have tried with no luck.

Reply

George A says:

July 11, 2017 at 12:22 pm

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.

Reply

ZUL says:

July 24, 2017 at 2:13 am

same here..Error coming saying method or data member not found..

Reply

vardis says:

August 15, 2017 at 8:41 am

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)

Reply

V Kumar says:

August 17, 2017 at 11:31 am

I typed the vba code but after ok nothing is happened. File error 2471 can help any please

Reply

accesshosting.com/create-login-form-ms-access 7/13
4/11/2019 How to Create a Login Form for MS Access

DENNIS WAGICHANGI says:

August 18, 2017 at 11:04 am

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

Reply

DENNIS WAGICHANGI says:

August 18, 2017 at 11:05 am

Check the spelling of the naming of your textboxes

Reply

Brewmaster says:

August 28, 2017 at 7:16 pm

The Password validation is not case sensitive. Any thoughts on fixing it?

Reply

Brewmaster says:

August 28, 2017 at 7:18 pm

you have to create the forms. The logic is already there to trigger forms named “Admin Form” and
“Navigation Form”.

Reply

Brewmaster says:

August 28, 2017 at 7:18 pm

Macro for “QuitAccess” appears to have been changed to “Exit”.

Reply

Lj says:

September 16, 2017 at 12:17 am

This works like magic, for the Me.txtuser error, you have to customize the right cell name so the code will
work. Thank you!

Reply

Pongpanod says:

September 20, 2017 at 10:38 am

I want link download this sample

Reply

accesshosting.com/create-login-form-ms-access 8/13
4/11/2019 How to Create a Login Form for MS Access

nemo says:

October 27, 2017 at 10:35 pm

its working but you can used the password of other users to log in

Reply

d says:

November 20, 2017 at 8:56 pm

I am getting an error “Ambiguous Name Detected: Command1_Click when opening the form.

Reply

Charles says:

November 22, 2017 at 4:57 am

its not working for my access 2007

Reply

Les says:

December 7, 2017 at 12:03 pm

“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?

Reply

Paul says:

December 20, 2017 at 4:15 pm

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.

Reply

Stew Williams says:

January 1, 2018 at 11:35 pm

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?

Reply

Jamila says:

February 2, 2018 at 4:22 am

thank you so much it works ,, how to get the username in other form ???

Reply

Mike says:

February 12, 2018 at 2:47 pm

What is the purpose of the UserLogin field?

Reply

accesshosting.com/create-login-form-ms-access 9/13
4/11/2019 How to Create a Login Form for MS Access

Gedeon Ibhraime Priniel Diamos says:

February 19, 2018 at 9:43 pm

Getting an error on the line the is Me.txtUserName.SetFocus under the if is null. please help

Reply

x says:

February 25, 2018 at 5:45 am

thanks for this.. works great!

Reply

db says:

March 7, 2018 at 2:53 pm

how would I retrieve that username to use on a form to display the current user ?

Reply

Tanya says:

March 13, 2018 at 12:10 pm

Error coming saying method or data member not found

Reply

Rui says:

May 12, 2018 at 9:24 pm

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.

Reply

Besrat Mekonnen says:

May 15, 2018 at 11:58 am

your the best

Reply

eranka says:

May 23, 2018 at 3:19 am

HI

Could you please explain frmUserinfo form?

Reply

accesshosting.com/create-login-form-ms-access 10/13
4/11/2019 How to Create a Login Form for MS Access

Houcine says:

June 6, 2018 at 7:52 am

Great work!!
Do you have an example of drag and drop files from explorer to listbox
Thank’s in advance

Reply

Chad says:

June 29, 2018 at 11:03 am

I have the same question

Reply

boaz nyauma says:

July 1, 2018 at 7:23 am

I am getting an error “Ambiguous Name Detected: Command10_Click when opening the form.

Reply

Héctor Dario Hernández Reinoza says:

July 5, 2018 at 9:11 pm

same here! have you solved the problem?

Reply

Héctor Dario Hernández Reinoza says:

July 5, 2018 at 9:13 pm

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”

accesshosting.com/create-login-form-ms-access 11/13
4/11/2019 How to Create a Login Form for MS Access
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
+++++++++++++++++++++++++++++++++++++++++++++

Reply

Gijsbert says:

July 10, 2018 at 5:58 am

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

Reply

Hana says:

October 1, 2018 at 3:55 am

Working perfectly…
thanks

Reply

Leave a Reply

Blog token not found.

accesshosting.com/create-login-form-ms-access 12/13
4/11/2019 How to Create a Login Form for MS Access

About Remote Desktop Hosting SharePoint 2013 Hosting SQL Server 2012 Hosting Sharepoint 2010 Hosting
Windows Dedicated Server Hosting Find the right Hosting Plan Backup Plans Blog MS Access Tips & Tricks Video
Tutorials Contact Free Trials Support Sharepoint Hosting FAQ Remote Desktop FAQ

© 2010-2019 Access Hosting LLC. All Rights Reserved | Sitemap | Privacy Policy | Acceptable Use Policy | Terms of Service
Microsoft ®, SharePoint ®, and Windows ® are all trademarks of Microsoft Corporation

accesshosting.com/create-login-form-ms-access 13/13