New: NextGenTemplates Academy - video courses on Excel formulas with AI and Pivot Tables & dashboards. Early-bird: 20% off with code EARLYBIRD20 - first 20 orders only. See the courses →
  • Learn Excel (Free Course)
    • 1. Shortcut Keys
    • 2. Fill & Flash Fill
    • 3. Find & Replace
    • 4. Sort & Filter
    • 5. Formulas & Functions
    • 6. Data Validation
    • 7. Conditional Formatting
    • 8. Basic Charts
    • 9. Advanced Charts
    • 10. Pivot Tables
    • 11. Excel Dashboards
    • 12. VBA Course
    • 13. Excel Formulas with AI
  • Templates
  • Tutorials
    • Charts and Visualization
    • VBA
    • Excel Tips and Tricks
    • Excel Dashboards
    • Power Pivot & DAX
  • Power BI
  • Template Store
    • Ready to Use Templates
  • Courses
  • Academy
    • Excel + AI Mastery: 100 Formulas
    • Excel Pivot Tables & Dashboards
  • About Us
  • Contact Us

PK: An Excel Expert

Transforming Data into Decisions

Home>Blogs>VBA>Login Form for Excel VBA Application with MS Access Database
Login Form for Excel VBA Application with MS Access Database
VBA

Login Form for Excel VBA Application with MS Access Database

12 Jun 2019
31458Comments Off on Login Form for Excel VBA Application with MS Access Database

A login form for an Excel VBA application gives your tool a professional front door: users type a user ID and password, and the form checks them against a user table before they get in. In this tutorial you build exactly the form from PK’s video: a UserForm designed in PowerPoint, connected to an MS Access database through an ADODB connection, with clear messages for a wrong ID or a wrong password. Before you start, one honest note: a VBA login form organises users and roles nicely, but it is not real security. You will see why, and what to use instead, further down.

Login form for Excel VBA application with User Login card, user ID and password boxes, Login and Clear buttons
The finished login form: a PowerPoint design used as a picture, with transparent text boxes on top.

Table of Contents

Toggle
  • Watch the Video Tutorial
  • What You Will Build
  • Part 1: Create the User Table in MS Access
    • Step 1: Create a blank database and a table
    • Step 2: Mask the password column
  • Part 2: Set Up the Workbook and the ADODB Reference
    • Step 3: Save a macro-enabled workbook
    • Step 4: Add the Microsoft ActiveX Data Objects reference
  • Part 3: Design the Login Form
    • Step 5: Insert the UserForm
    • Step 6: Draw the design in PowerPoint
    • Step 7: Match the form background colour
    • Step 8: Add the picture
    • Step 9: Add labels for the heading and buttons
    • Step 10: Add the two text boxes
    • Step 11: Name the buttons
  • Part 4: The Login Form VBA Code
    • Step 12: Validate the boxes on the Login button
    • Step 13: Write the User_Login procedure
  • How the Login Code Works
    • The connection string
    • The query and the recordset
    • Three outcomes
    • Step 14: Log in with the Enter key
    • Step 15: The Clear button
  • Is a VBA Login Form Secure? An Honest Answer
  • Common Errors and Fixes
    • Compile error: User-defined type not defined
    • Provider cannot be found or is not registered
    • Could not find file
  • Tips
  • Want It Ready-Made?
  • Related Tutorials
  • Frequently Asked Questions
    • How do I create a login form in Excel VBA?
    • Can a VBA login form protect my Excel data?
    • Why connect the login form to MS Access instead of a worksheet?
    • Which reference do I need for the ADODB connection?
    • How do I make the Enter key log the user in?
  • About the Author
  • Conclusion

Watch the Video Tutorial

PK creates the Access user table, designs the form in PowerPoint, places it on a UserForm with transparent text boxes, then writes the ADODB login procedure and the Enter-key shortcut.

What You Will Build

  • An Access user table (TBL_UM) with ID, User_Id, User_Name, Password and Role.
  • A designer UserForm called frm_Login, built from a PowerPoint snapshot.
  • A User_Login procedure that opens an ADODB connection and checks the ID and password.
  • Validation messages for an empty box, an incorrect user ID and an incorrect password.
  • Enter-key login from either text box, plus a Clear button.

Part 1: Create the User Table in MS Access

Step 1: Create a blank database and a table

Open MS Access, create a Blank database and save it in the folder of your project. Go to Create → Table Design and add these fields:

  • ID: AutoNumber, set as the Primary Key.
  • User_Id, User_Name, Password and Role: Short Text.

Role separates an Admin from a normal User, and you can add columns such as an email ID. Save the table; in PK’s sample it is called TBL_UM.

MS Access table TBL_UM with ID, User_Id, User_Name, Password and Role fields for the VBA login form
The TBL_UM table with two users: Admin123 (role Admin) and User123 (role User).

Step 2: Mask the password column

Typed passwords are visible in the table, so PK masks them. Open the table in Design View, click the Password field, click the three dots next to Input Mask and choose Password in the Input Mask Wizard. Save, and the column now shows asterisks. Keep in mind that this only hides the display: the value is still stored as plain text.

Part 2: Set Up the Workbook and the ADODB Reference

Step 3: Save a macro-enabled workbook

Create a new workbook and save it as .xlsm (macro-enabled) or .xlsb (binary). PK uses .xlsb. Keep the database in a Database subfolder next to the workbook, because the code finds it from the workbook’s own path.

Step 4: Add the Microsoft ActiveX Data Objects reference

Press Alt+F11 to open the Visual Basic Editor, then go to Tools → References and tick Microsoft ActiveX Data Objects 2.8 Library. Without this reference VBA does not know what an ADODB.Connection is.

VBA References dialog with Microsoft ActiveX Data Objects 2.8 Library ticked for the ADODB connection
Tools → References → Microsoft ActiveX Data Objects 2.8 Library.

Part 3: Design the Login Form

Step 5: Insert the UserForm

Choose Insert → UserForm and make it a little bigger. In the Properties window set (Name) to frm_Login and Caption to Login Form.

Step 6: Draw the design in PowerPoint

VBA controls look plain, so PK draws the login card in PowerPoint: rounded rectangles for the boxes and buttons, a coloured header strip, user and lock icons and a footer line. Each element is a separate shape, so you can change any colour. When you are happy, set the slide zoom to 100%, capture the card with the Snipping Tool (take it slightly wider than the card) and save the image.

Step 7: Match the form background colour

The slide background in PK’s design is RGB 2, 86, 112 (check it in PowerPoint under Format Background → Color → More Colors). Give the UserForm the same colour in code:

Private Sub UserForm_Activate()

    Me.BackColor = RGB(2, 86, 112)

End Sub

Alternatively, open the form’s BackColor property, go to the Palette, right-click a white box to open Define Color, type the RGB values and click Add Color.

Step 8: Add the picture

Add an Image control, set BackStyle to Transparent and BorderStyle to None, then load your snapshot in the Picture property. The design is now on the form, but it is only a picture, so the boxes and buttons do nothing yet.

Step 9: Add labels for the heading and buttons

Add a Label for the User Login heading with a transparent BackStyle, a bigger font and centred text. Copy it onto each button for the Login and Clear captions, use a smaller font (about 9) and nudge them into place with the Top property.

Step 10: Add the two text boxes

Drag a TextBox over the first rounded box. Set the Height to about 20, BackStyle to Transparent, BorderStyle to None, SpecialEffect to Flat and the font to Bold. Run the form and adjust Top a few points until the text sits in the middle of the rounded box (PK moved it from 120 to 124). Copy it for the password box and align both to the left.

For the second box (TextBox2) type an asterisk in the PasswordChar property, so whatever the user types appears as stars.

Step 11: Name the buttons

The Login and Clear labels sitting on the button shapes act as the buttons. In the Properties window rename them btn_Login and btn_Clear, so their Click procedures read clearly in the code.

Part 4: The Login Form VBA Code

Step 12: Validate the boxes on the Login button

Double-click the Login button and check that neither box is empty before you touch the database:

Private Sub btn_Login_Click()



    If Me.TextBox1.Value = "" Then

        MsgBox "Please enter the User Id", vbCritical

        Exit Sub

    End If



    If Me.TextBox2.Value = "" Then

        MsgBox "Please enter the Password", vbCritical

        Exit Sub

    End If



    Call User_Login(Me.TextBox1.Value, Me.TextBox2.Value)



End Sub

Step 13: Write the User_Login procedure

Insert a standard module (PK calls it Login_Module) and add the procedure that talks to Access. This is the code from the original tutorial:

Option Explicit



Sub User_Login(User_Id As String, Password As String)



Dim cnn As New ADODB.Connection

Dim rst As New ADODB.Recordset



Dim qry As String



cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Database\Database.accdb"



qry = "SELECT * FROM TBL_UM WHERE User_id = '" & User_Id & "'"

rst.Open qry, cnn, adOpenKeyset, adLockOptimistic



If rst.RecordCount = 0 Then

MsgBox "Incorrect User Id", vbCritical

ElseIf rst.Fields("Password").Value = Password Then

MsgBox "Login successfully", vbInformation

Else

MsgBox "Incorrect password", vbCritical

End If



rst.Close

cnn.Close



End Sub

How the Login Code Works

The connection string

cnn.Open connects to the Access file. The provider part, Microsoft.ACE.OLEDB.12.0, stays the same for .accdb files. Data Source is the full path of the database. Using ThisWorkbook.Path keeps the path dynamic: move the whole folder and the form still finds the database.

The query and the recordset

The query selects the row whose User_Id matches what was typed. rst.Open runs it on the connection with the adOpenKeyset cursor and the adLockOptimistic lock type. A keyset cursor gives a reliable RecordCount, which the next line needs. For a large database or many users, pick the cursor and lock type carefully; the ADODB connection chapter explains every option.

Three outcomes

  • RecordCount = 0: no user with that ID exists, so the message is Incorrect User Id.
  • Password field matches: the login succeeds. This is where your real application continues, for example by hiding the login form and showing the main form.
  • Otherwise: the ID exists but the password does not match, so the message is Incorrect password.

Finally the recordset and the connection are closed, so the database file is not left locked.

Step 14: Log in with the Enter key

Users expect Enter to work. Add a KeyDown event to each text box; key code 13 is Enter:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Then btn_Login_Click

End Sub



Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Then btn_Login_Click

End Sub

Step 15: The Clear button

The Clear button simply empties both boxes. The video does not show its lines, so here is a minimal version:

Private Sub btn_Clear_Click()

    Me.TextBox1.Value = ""

    Me.TextBox2.Value = ""

    Me.TextBox1.SetFocus

End Sub

Test it: a wrong ID shows Incorrect User Id, a right ID with a wrong password shows Incorrect password, and the correct pair shows Login successfully.

Is a VBA Login Form Secure? An Honest Answer

This section is an addition from general Excel and Access knowledge; it is not covered in the video.

No. A login form in Excel VBA is a convenience and role-management feature, not a security control. Here is why:

  • Macros can be bypassed. Anyone who opens the workbook with macros disabled never sees the form, and the data on the sheets is right there.
  • VBA project passwords and sheet or workbook protection are weak. They stop accidents, not a determined user; tools to remove them are easy to find.
  • The passwords are stored as plain text. The Access input mask hides them on screen only. Anyone who can open the .accdb file can read them.
  • The query is built by joining text. A user ID containing a single quote breaks the query, and crafted input could change it (SQL injection).

If you keep the form, harden it: never reuse real network passwords, put the database in a folder only authorised users can reach, and use a parameterised query instead of text joining. These lines replace the qry = ... and rst.Open qry, ... lines in User_Login; adVarWChar (value 202) and the other constants come from the ActiveX Data Objects reference you added in Step 4:

Dim cmd As New ADODB.Command

Set cmd.ActiveConnection = cnn

cmd.CommandText = "SELECT * FROM TBL_UM WHERE User_Id = ?"

cmd.Parameters.Append cmd.CreateParameter("uid", adVarWChar, adParamInput, 255, User_Id)

rst.CursorType = adOpenKeyset

rst.Open cmd

For data that really must be protected, rely on things outside VBA: Windows or SharePoint folder permissions, file encryption with a strong password (File → Info → Protect Workbook → Encrypt with Password), or a proper database server with its own user accounts.

Common Errors and Fixes

Additions from general VBA practice; not shown in the video.

Compile error: User-defined type not defined

The ActiveX Data Objects reference is missing. Add it under Tools → References (2.8 or the newest 6.x library both work).

Provider cannot be found or is not registered

The Access Database Engine (ACE) is not installed, or its 32/64-bit version does not match Office. Install the Microsoft Access Database Engine that matches your Office bitness.

Could not find file

The database is not where the connection string says. With ThisWorkbook.Path, the file must be in the Database subfolder and the workbook must be saved first.

Tips

  • Show the login form from Workbook_Open, and set the form’s ShowModal property to True so nothing else can be clicked behind it.
  • Store the logged-in user’s name and role in a public variable to enable or hide Admin features.
  • Microsoft documents the connection object in the ADO Connection object reference.

Want It Ready-Made?

If you would rather start from the finished file, the Login Form for Excel VBA based Application on NextGenTemplates.com includes the designed form, the Access database and the code from this tutorial. For complete VBA applications and utilities, browse the Excel VBA tools collection.

Related Tutorials

  • VBA ADODB Connection: Connect Excel to Access and SQL Databases
  • VBA Application with MS Access Database
  • 5 VBA UserForm Super Tips
  • Data Entry Form with Picture Box in Excel VBA
  • Excel VBA Course: 18 Chapters from Macro Recording to ADODB

Frequently Asked Questions

How do I create a login form in Excel VBA?

Insert a UserForm with a user ID box, a password box with PasswordChar set to an asterisk and a Login button. The button calls a procedure that looks up the user in a table and compares the password, as shown in Part 4.

Can a VBA login form protect my Excel data?

Not on its own. Macros can be disabled and VBA or sheet passwords can be removed, so treat the form as user and role management. Protect sensitive data with folder permissions or file encryption.

Why connect the login form to MS Access instead of a worksheet?

Keeping users in a separate Access file means several workbooks can share one user list, and you can add or remove users without editing the Excel file.

Which reference do I need for the ADODB connection?

Microsoft ActiveX Data Objects, added in the Visual Basic Editor under Tools and References. The video uses version 2.8; newer 6.x versions also work.

How do I make the Enter key log the user in?

Add a KeyDown event to each text box and call the Login button procedure when the key code is 13, which is the Enter key.

About the Author

PK is a Microsoft Excel expert and trainer and the founder of PK: An Excel Expert and NextGenTemplates.com. He has been teaching Excel, VBA, Power Query and Power BI since 2016 on his YouTube channel.

Conclusion

You now have a complete login form for an Excel VBA application: an Access user table, a PowerPoint-designed UserForm with transparent text boxes, a User_Login procedure that checks the ID and password through ADODB, and Enter-key support. Use it to give your tools a polished start screen and role-based features, and remember that real protection for sensitive data comes from permissions and encryption, not from VBA.

Tagged ADODB, Data Entry, Data Entry form in VBA, Login form, MS Access, Power Point, User Form
PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com

Related Articles

Templates VBA

Home Nursing Visit Calendar in Excel

September 1, 2026September 1, 2026 PK
Home nursing runs on a schedule that almost never fits a wall planner. A four-weekly catheter change, the third of
Templates VBA

Training Completion Certification Tracker in Excel

July 31, 2025July 31, 2025 PK
In today’s fast-paced and compliance-driven work environment, tracking employee training is no longer optional—it's essential. Whether you're an HR manager,
VBA

Ayurveda Centre Management System V1.0 in Excel

July 29, 2025July 29, 2025 PK
Running an Ayurveda center involves managing various elements—appointments, treatments, therapists, patients, and finances. Without a proper system, this can get

Post navigation

Liquid Fill Gauge in Power BI: Custom Visual Setup Guide
Shell Function in VBA: Open Programs, Folders and Files

Join Our Course

  • Excel Pivot Tables for Data Analysis

Top Categories

  • Blogs (66)
  • Charts and Visualization (158)
  • Dashboard (3,145)
  • Excel Tips and Tricks (198)
  • Power BI (1,542)
  • Power Pivot (123)
  • Power Point Visualization (4)
  • Power Query (3)
  • Predictive Analytics (5)
  • Productivity (2)
  • Templates (2,238)
  • VBA (369)
instant= new adsenseLoader( '#quads-ad1-place', { onLoad: function( ad ){ if (ad.classList.contains("quads-ll")) { ad.classList.remove("quads-ll"); } } });

Latest Articles

Retail Shop Business Plan: 35-File Starter Templates Kit

October 10, 2026 PK

Billing System in Excel: Build an Invoice System Step by Step (No VBA)

October 8, 2026 PK

Stationery Stores Dashboard in Excel

October 8, 2026 PK

General Store Billing Management System in Excel VBA

October 8, 2026October 11, 2026 PK

Sporting Goods Dashboard in Power BI

October 7, 2026 PK

Popular Articles

Construction and Real Estate Regulatory Adherence KPI Dashboard in Excel

August 28, 2025August 28, 2025 PK

Online Shopping Checklist in Excel

January 26, 2025March 19, 2025 PK

How to use TOCOL function in Excel

September 2, 2024October 10, 2026 PK

Maternity Hospitals KPI Dashboard in Excel

March 16, 2026March 16, 2026 PK

Human Resources Resource Allocation KPI Dashboard in Excel

April 27, 2025April 27, 2025 PK

Learn More

  • Free Excel Course
  • VBA Course
  • Excel Dashboards
  • Charts and Visualization
  • VBA Tutorials
  • Excel Tips and Tricks
  • Power BI Tutorials
  • Courses

Subscribe to our Newsletter



Free Excel Course

  • 1. Shortcut Keys
  • 2. Fill and Flash Fill
  • 3. Find and Replace
  • 4. Sort and Filter
  • 5. Formulas and Functions
  • 6. Data Validation
  • 7. Conditional Formatting
  • 8. Basic Charts
  • 9. Advanced Charts
  • 10. Pivot Tables
  • 11. Excel Dashboards
  • 12. VBA Course
  • 13. Excel Formulas with AI

See the full learning path →

instant= new adsenseLoader( '#quads-ad2-place', { onLoad: function( ad ){ if (ad.classList.contains("quads-ll")) { ad.classList.remove("quads-ll"); } } });

Popular Tags

Business Analytics business intelligence checklist template data analysis Data Analytics Data Entry data entry excel Data Entry form Data Entry form in VBA Data visualization Event Management Excel excel-kpi-template Excel calendar Excel Calendar Template excel checklist Excel Dashboard Excel dashboard template Excel KPI Dashboard Excel reporting Excel Template Excel Templates Excel Tips Excel Tutorial healthcare analytics HR Analytics KPI Dashboard KPI dashboard Excel KPI tracking MTD YTD analysis MTD YTD dashboard MTD YTD KPIs performance dashboard Performance Metrics performance tracking Power BI Power BI dashboard Power BI KPI Dashboard Power BI Reporting Power BI reports Power BI Template Power BI Templates Project Management Task Management VBA
  • Learn Excel (Free Course)
    • 1. Shortcut Keys
    • 2. Fill & Flash Fill
    • 3. Find & Replace
    • 4. Sort & Filter
    • 5. Formulas & Functions
    • 6. Data Validation
    • 7. Conditional Formatting
    • 8. Basic Charts
    • 9. Advanced Charts
    • 10. Pivot Tables
    • 11. Excel Dashboards
    • 12. VBA Course
    • 13. Excel Formulas with AI
  • Templates
  • Tutorials
    • Charts and Visualization
    • VBA
    • Excel Tips and Tricks
    • Excel Dashboards
    • Power Pivot & DAX
  • Power BI
  • Template Store
    • Ready to Use Templates
  • Courses
  • Academy
    • Excel + AI Mastery: 100 Formulas
    • Excel Pivot Tables & Dashboards
  • About Us
  • Contact Us
Copyright © 2023 PK-AnExcelExpert.com - Privacy Policy
This website uses cookies to improve your user experience, analyze site traffic and serve targeted ads in accordance with our Privacy Policy ACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT