Home > Publications & Training > Case Studies > New and Check Password
Web This Site

Password Userform Module

This was inspired by Dick Kusleika’s post Masking Passwords (http://www.dailydoseofexcel.com/archives/2011/07/12/masking-passwords/).  It’s an enhanced version of DK’s approach in the sense that the same userform module supports both entering a new password as well as entering a password for confirmation.  There are other enhancements.  The user can choose to either mask or unmask the password.  The developer has more choices about what is and is not allowed in the password.

Download the Excel workbook in a zip file

The user interface

My experiences with password fields heavily influenced the user interface.  It is not always necessary to mask the password.  When I am home, or in my home office, or in a hotel room, or in an otherwise secure environment, I have no problems entering an unmasked password.  And, of course, when I do that, I don’t believe that there is a reason to verify the password by typing it a second time – but, maybe there is.  In any case, the resulting UI is described below.

Entering a new password

When entering a new password, if the password is masked then we do need to verify the password by entering it a second time.

Figure 1 – Enter a new password when the password field is masked.
With the field masked, the consumer has to verify the password by entering it twice.

If, however, the field contents are visible, then there is no need to verify the password…or is there?

Figure 2 – Entering a new password without masking eliminates the need to enter it twice…or does it?

If there is an error in the password in the sense that the minimum requirements specified by the developer are not met, an explicit message is shown to the user.

Verifying an existing password

When checking the user’s password, there is never a need to have the password entered more than once.

Figure 3 – Verifying the consumer knows the correct password never requires entering it twice

If there is an error in the password the only message shown is a generic “invalid password.”

The programmatic interface

There are several options associated with verifying an acceptable password.  This module addresses only those that do not require checking with existing data or historical behavior.  The customizable elements include:

·        What is the minimum password length?

·        Is at least one special character (not a letter and not a number) required?

·        Is at least one letter required?

·        Is at least one uppercase letter required?  If so, at least one lowercase letter will be automatically required.

·        Is at least one number required?

The userform module has a single programmatic entry point described in .  It controls whether the required password is a new password or if it is an existing password.  It also controls each of the options described above.

The function returns True if a user provided a password, and if so the password is in the uPWD argument.

Public Function getPWD(ByRef uPWD As String, _

        ByVal uNewPWD As Boolean, _

        Optional ByVal ulMINLENGTH As Long = 6, _

        Optional ByVal uSpecialCharReqd As Boolean = True, _

        Optional ByVal uLetterReqd As Boolean = True, _

        Optional ByVal uUpCaseReqd As Boolean = True, _

        Optional ByVal uNumberReqd As Boolean = True) As Boolean

Code Sample 1

As far as the code in the userform module goes, the interested reader is welcome to examine it.  Just follow the instructions in the section below.

Using the code

Download the zip file that contains the workbook that contains the userform.  Unzip the zip file and save the workbook.  Then, open the workbook.  You may get a warning about the file containing macros.  You may choose to disable macros so that you can examine the workbook’s VBA project.

Import the ufPWD userform into the workbook where you will use it.  Then, use Code Sample 2 as a guide as to how to use the code.

Option Explicit


Sub Test_GetPassword()


    Dim PWD As String


    If ufPWD.getPWD(PWD, True) Then MsgBox PWD

    If ufPWD.getPWD(PWD, False) Then MsgBox PWD


    End Sub

Code Sample 2 – This code goes in a standard module



Share Your Comments