Part 5 – Secure Your Login System in MS Access

In order to protect your data, it is crucial to strengthen and secure your login system in Microsoft Access. Implementing strong security measures not only protects sensitive information but also prevents unauthorized access, reducing potential risks and vulnerabilities. By setting up strict authentication protocols and access controls, you can strengthen the security of your login system and build trust in the integrity of your database.

Overview

While registering a new user we have already applied the user verification by 5 digits of his phone in Part 4 of the Ultimate Login System. In this 5th part, you will learn how to force a user to enter a unique username, meet the password criteria, and maintain the password strength. We will cover these key features:

  • Avoid duplicate usernames
  • Password Secured Criteria
  • Establish Password Strength
Secure Your Login System

Steps to Secure Your Login System

You will require an instructions box for the user to show the minimum password character length and the password criteria to include Uppercase, Lowercase, Number, and a special character in the password.

In the second column, create three text boxes. The first textbox is for the username, and the others are for the password and password verification.

Create a label to change the caption for the Password Strength as Weak, Medium, or Strong Password. Colors will be changed according to the password strength.

Password Mask Issue While Clicking Outside

Suppose you are going through an abnormal default behavior of the Password visibility while clicking on any other button. In that case, you can solve this issue by applying a simple code from the “After Update” event. Here is the code:

Private Sub txt_password_AfterUpdate()
Me.txt_password.Format = ""
End Sub

Unique Method to Avoid Duplicate Username

Insert a label under the Username text box and 2 icons reflecting OK and Not OK. Rename and hide them from the property sheet. Start VBA coding from the “After Update” event of the Username text box and insert this code:

Private Sub r_username_AfterUpdate()
'Check if Username already exists
If DCount("uusername", "UserTable", "uusername= '" & Me.r_username & "'") > 0 Then
Me.ImgUsernameError.Visible = True
Me.LblAlreadyTaken.Visible = True
Me.r_password.SetFocus
Exit Sub
Else
Me.ImgUsernameOk.Visible = True
Me.LblAlreadyTaken.Visible = False
Me.r_password.SetFocus
End If
End Sub

To hide both icons while clicking on the Username text box again for the retry username, insert this code from the “On Got Focus” event:

Private Sub r_username_GotFocus()
'While focusing on Username Textbox, both Icons should be hidden.
Me.ImgUsernameOk.Visible = False
Me.ImgUsernameError.Visible = False
Me.LblAlreadyTaken.Visible = False
End Sub

Outcome: The red/cross icon should appear if the user already exists and the green/ok if the user is new. Both icons should be hidden in case of textbox highlights.

Unique Method to Mask and Unmask the Password

Insert 2 buttons with both password text boxes and apply icons. Make the background of the buttons transparent with no theme. Apply Password Mask to both text boxes. Apply this code to the “On Click” event of the first text box.

Private Sub cmdPassword1_Click()
If (Me.r_password.InputMask = "Password") Then
Me.r_password.InputMask = False
Else
Me.r_password.InputMask = "Password"
End If
End Sub

Unique Method to Determine the Password Strength

Create a textbox, name it “txtCount” which will be used to count the password character length including 4 eligibility criteria to include uppercase, lowercase, number, and special characters.

Use the ASCII characters to determine which type of character is pressed. You can download our customized chart of ASCII characters and codes which is designed for you to understand better.

Apply this code to the “On Change” event of the password textbox.

Private Sub r_password_Change()
On Error Resume Next

'Focus other text box than password
Me.txtCount.SetFocus
'Number of Character length
TL = Nz(Len(Me.r_password), 0)

Dim i As Integer
Dim s As Integer
i = 0

For i = 1 To TL
'Find the 4 method Characters using ASCII codes
s = Asc(Mid(Me.r_password, i, 1))

'There should be 1 upper, 1 lower, 1 number and 1 special character
Select Case s
Case 65 To 90 'for Uppercase
upper = 1
Case 97 To 122 'for Lowercase
lower = 1
Case 48 To 57 'for Numbers
nm = 1 'We can not use Number as variable due to built in
Case 33 To 47, 58 To 64, 91 To 96, 123 To 126
special = 1
Strong = Strong + 1 'Number of increased special characters will make the password strong
End Select
Next i
L = TL + Strong 'Like this the Special character will count as 2

cnt = upper + lower + nm + special 'If the value is not 4, the password will not be acceptable
Me.txtCount = cnt

'The Password next cursor to the next character will be highlighted with this method
'Focus back to password text box
Me.r_password.SetFocus

'It will decide the length to move th corsor to the end of the password text box
If Not IsNull(Len(Me.r_password)) Then
Me.r_password.SelStart = Len(Me.r_password)
End If

'7 for other than Special Character and add 2 for Special characters
'It means, 8 character length requried, but 9 for strong will be considered as a weak password
If IsNull(Me.r_password) = True Then
Me.LblPasswordStrength.Caption = " "
Me.LblPasswordStrength.BackColor = 13421772
ElseIf L > 9 And L <= 10 Then
Me.LblPasswordStrength.Caption = "Weak Password"
Me.LblPasswordStrength.BackColor = vbRed
ElseIf L > 10 And L <= 12 Then
Me.LblPasswordStrength.Caption = "Medium Password"
Me.LblPasswordStrength.BackColor = vbYellow
ElseIf L > 12 Then
Me.LblPasswordStrength.Caption = "Strong Password"
Me.LblPasswordStrength.BackColor = vbGreen
End If
End Sub

Finally, an additional code in the Submit button is required to verify that all 4 eligibility characters are added to the password. Here is the complete code of the Submit button with the addition:

Private Sub Command17_Click()
'Check null textboxes
If IsNull(Me.r_name) = True Or IsNull(Me.r_phone) = True Or IsNull(Me.r_username) = True Or IsNull(Me.r_password) = True Or IsNull(Me.r_vpassword) = True Then
MsgBox ("Mandatory fields must be filled !"), , "| Dara Required |"
Exit Sub
Else
'Verify the length of the last 5 digits of phone
If Len(Me.r_phone) > 5 Then
MsgBox "The phone digits must be at least 5 numbers", , "| Phone Digits limits exceed |"
Exit Sub
Else
'Verification of all 4 criteria
If Me.txtCount < 4 Then
MsgBox "The Password criteris must be meet as per requirement before proceeding", vbCritical, "|Password not accepted|"
Exit Sub
Else
'Verify both passwords are matched
If Me.r_password <> Me.r_vpassword Then
MsgBox "Password do not match...", , "| Password must be matched |"
Exit Sub
Else
'Proceeding to store data into the table
Set rst = CurrentDb.OpenRecordset("UserTable", dbOpenDynaset, dbSeeChanges)
With rst
.AddNew
.Fields("uname") = Me.r_name
.Fields("uphone") = Me.r_phone
.Fields("udate") = Date
.Fields("ugender") = Me.r_gender
.Fields("uage") = Me.r_age
.Fields("uusername") = Me.r_username
.Fields("upassword") = Me.r_password
.Fields("ustatus") = "User"
.Update
End With
Set rst = Nothing
'Closing the form
DoCmd.Close acForm, "RegisterUser", acSavePrompt
End If
End If
End If
End If
End Sub

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *