Password Complexity Code (1 Viewer)

mba_110

Registered User.
Local time
Today, 15:59
Joined
Jan 20, 2015
Messages
280
Hi

I have found the following code, i am trying to modify it as per my requirement for password complexity.

my requirement are.

Password must have 8 digits in total including one Capital letter, one numeric and one special character.

Code:
Option Compare Database
Option Explicit
 
Enum pwRequired
    pwNum = 1      ' Require at least one numeric character
    pwAlpha = 2    ' Require at least one Alpha character
    pwMixCase = 4  ' Require at least one of each both upper and low case characters
    pwSpecial = 8  ' Require at least one special character
End Enum
 

' Purpose   : Test a string for:
'             Minimum number of characters
'             Presence of at least one of each of the designated character groups
' Usage     : Enter the minimum number of characters as the MinLength Argument
'             Add together each type of pwRequired type to the ReqChars argument
'             (pwAlpha is not required with pwMixCase)
'             Optionally set UseDialog to True to pop up a message about missing requirements
' Example   : RequiredCharacters(YourString, 4, pwNum + pwMixCase + pwSpecial, True)


Public Function RequiredCharacters(ByVal TestString As String, ByVal MinLength As Integer, ByVal ReqChars As pwRequired, _
                            Optional ByVal UseDialog As Boolean = False) As Integer
 
' RETURN CODES:
' Characters Accepted = 0
 
' SUMMED RETURN CODES:
' Numeral required = 1
' Alpha or Lowercase character required = 2
' UpperCase character required = 4
' Special character required = 8
' Insufficient Characters = 16
 
Dim StringLen As Integer
Dim Char As Integer '.... ASCII value of character
Dim i As Integer
 
' Load pwAlpha if pwMixCase                (effect: require lowercase)
' Otherwise render TestString to lowercase (effect: detect uppercase as lower)
    If ReqChars <> (ReqChars And Not pwMixCase) Then
        ReqChars = ReqChars Or pwAlpha
    Else
        TestString = LCase(TestString)
    End If
 
' Include all active pwRequired values and length code in function return
    RequiredCharacters = ReqChars Or 16
 
    StringLen = Len(TestString)
 
' Remove each found RequiredCode from function return
 
    If Not StringLen < MinLength Then: RequiredCharacters = RequiredCharacters And Not 16
 
    For i = 1 To StringLen
        Char = Asc(Mid(TestString, i, 1))
 
        If (Char > 46 And Char < 58) Then                              ' Numeric
            RequiredCharacters = RequiredCharacters And Not pwNum
        ElseIf (Char > 96 And Char < 123) Then                         ' LowerCase
            RequiredCharacters = RequiredCharacters And Not pwAlpha
        ElseIf (Char > 64 And Char < 91) Then                          ' UpperCase
            RequiredCharacters = RequiredCharacters And Not pwMixCase
        Else                                                           ' Special
            RequiredCharacters = RequiredCharacters And Not pwSpecial
        End If
    Next
 
    If UseDialog And RequiredCharacters Then: RequiredCharsDialog (RequiredCharacters)
 
End Function
 
=======================================
Public Function RequiredCharsDialog(RequiredCode As Integer)
 
' Note: When using pwAlpha the "missing requirement" will be "At least one lowercase character"
'       even though an uppercase character would be accepted.
 
Dim NotPresent As String
 
    If RequiredCode <> 0 Then
        NotPresent = "The string still requires:" & vbCrLf & vbCrLf
        If (RequiredCode And Not 15) = 16 Then: NotPresent = NotPresent & "Additional characters" & vbCrLf
        If (RequiredCode And Not 23) = 8 Then: NotPresent = NotPresent & "At least one special character" & vbCrLf
        If (RequiredCode And Not 27) = 4 Then: NotPresent = NotPresent & "At least one uppercase Character" & vbCrLf
        If (RequiredCode And Not 29) = 2 Then: NotPresent = NotPresent & "At least one lowercase Character" & vbCrLf
        If (RequiredCode And Not 30) = 1 Then: NotPresent = NotPresent & "At least one numeric character" & vbCrLf
 
        MsgBox NotPresent
 
    End If
 
End Function
 
=================================
Private Sub TestRequiredCharacters()
    If RequiredCharacters("1aA!", 4, pwNum + pwMixCase + pwSpecial, True) = False Then
        MsgBox "Accepted"
    End If
 
End Sub
 
' =============   Notes   =================
' The functions make extensive use of the Bitwise Operators.
' Each pwRequired value uses a single bit to indicate its inclusion in ReqChar.
' (Hence the binary pattern in their decimal values)
' The bitwise operators allow the bits to be operated on independently.
' This makes them useful where multiple independent values are held as a single number.
 
' X Or Y adds the bits without "carrying" as in arithmetic addition.
 
' X And Not Y subtracts the bits in the second operand from the first.
' Effectively this is subtraction without "borrowing".
' ==========================================


Can anyone help me please with this to modify for my above requirement.

Thanks.
 

Minty

AWF VIP
Local time
Today, 23:59
Joined
Jul 26, 2013
Messages
10,378
The code will do exactly what you want, simply specify the criteria as per the example for your requirements;

Code:
RequiredCharacters(YourString, 8, pwNum + pwMixCase + pwSpecial, True)
 

mba_110

Registered User.
Local time
Today, 15:59
Joined
Jan 20, 2015
Messages
280
This is the code i am trying to execute.


Code:
Private Sub txtConfirmPassword_AfterUpdate()
If Nz(Me.txtNewPassword, "") = "" Then
MsgBox "New Password is required", vbOKOnly, "Invalid Entry!"
Else
Me.txtNewPassword = RequiredCharacters
End If
End Sub

Private Sub txtNewPassword_AfterUpdate()
If RequiredCharacters(YourString, 8, pwNum + pwMixCase + pwSpecial, True) Then

Else
Me.txtConfirmPassword.Visible = False
Me.txtNewPassword.SetFocus
End If
End Sub

Private Sub txtNewPassword_BeforeUpdate(Cancel As Integer)
If Nz(Me.txtOldPassword, "") = "" Then
        MsgBox "Old Password is required", vbOKOnly, "Invalid Entry!"
        Me.txtConfirmPassword.Visible = False
        Exit Sub
 End If
End Sub

but giving error of compile error argument is not optional, may be i have not put the code in order.

Kindly spot the problem to fix it.

thanks.
 

Minty

AWF VIP
Local time
Today, 23:59
Joined
Jul 26, 2013
Messages
10,378
Your code as listed doesn't make much sense.
You haven't attempted to use the code supplied using your form controls.

The process should be something like

On Form Load the Confirm Password box should be disabled.

Check the first "New" password textbox conforms to your criteria, if not return to that text box. You are sort of close here ;

Code:
Private Sub txtNewPassword_AfterUpdate()
 If RequiredCharacters([COLOR="Red"]Me.txtNewPassword[/COLOR], 8, pwNum + pwMixCase + pwSpecial, True) Then
      Me.txtConfirmPassword.Enabled= True
 Else
      Me.txtConfirmPassword.Enabled= False
      Me.txtNewPassword.SetFocus    [COLOR="Green"]  ' Not sure you can set focus back to the control you just left ?[/COLOR]
 End If
End Sub

If the new password meets your criteria, you simply need to compare the second one to the first one using a case sensitive check, no need to check it meets the complexity again, you've already done that.

Then save the new password to the users ID.
 

mba_110

Registered User.
Local time
Today, 15:59
Joined
Jan 20, 2015
Messages
280
Thanks i have fixed that, now moving forward i need to update the records in tblUserSecurity for the following fields based on [frmEmpResetPassword]![txtRsetLoginEmp]

The fields are
StrPassword
ResetDate
PED


Table fields are.

LoginID - Text field (PK)
StrPassword - Text Field
EmpID - Number field
ResetDate - date field
PED - Number


Forms fields to table fields are.

txtRsetLoginEmp - [LoginID]
txtConfirmPassword - [strPassword]
txtResetDate - [ResetDate]
txtPasswordExpiryDays - [PED]


The sql i try to make is for one field only, but i need it for all three fields like i mentioned above (strPassword,ResetDate,PED)

Code:
Private Sub BtnSave_Click()
Dim strSQL As String
 
strSQL = "UPDATE tblUserSecurity SET tblUsersecurity![strpassword] = Forms!frmEmpResetPassword![txtConfirmPassword], tblUserSecurity![LoginID] = Forms!frmEmpResetPassword![txtConfirmPassword] ,tblUserSecurity![strpassword] = Forms!frmEmpresetPassword![strpassword]" _
& "WHERE tblUserSecurity![LoginID] = Forms!frmEmpResetPassword![txtRsetLoginEmp]"
 
If Me.Dirty Then Me.Dirty = False
DoCmd.RunSQL (strSQL)
Me.Requery
Me.txtRsetLoginEmp.Requery
MsgBox "You have sucessfully updated the password"
Exit Sub
End If

If Me.txtPasswordExpiryDays = "" Then
MsgBox "Kindly mention your password expiry days, if you wish to keep then mention zero"
Me.txtPasswordExpiryDays.SetFocus
Exit Sub
End If
End Sub

Many thanks for your help.
 

isladogs

MVP / VIP
Local time
Today, 23:59
Joined
Jan 14, 2017
Messages
18,258
Add all 3 fields to your SQL update statement and add delimiters to the forms part of the WHERE clause
 

mba_110

Registered User.
Local time
Today, 15:59
Joined
Jan 20, 2015
Messages
280
Sorry i am not sure what are the exact ways of adding it, can you please help to make, also i am not sure the way i have make the update SQL need full code review.

Thanks.
 

Minty

AWF VIP
Local time
Today, 23:59
Joined
Jul 26, 2013
Messages
10,378
You need to learn how to do this if you want to use VBA queries.

Create the update query in the query editor. You can either save that and simply run it, or

Go to SQL view.

That will give you the basic correct SQL text.

Paste that into your VBA, and then edit it to concatenate the form values for the data types correctly - use ' ' around text fields, and # # around dates ensuring they are formatted using yyyy-mm-dd.

If it doesn't work use debug to assist you - instruction listed here http://www.baldyweb.com/ImmediateWindow.htm
 

isladogs

MVP / VIP
Local time
Today, 23:59
Joined
Jan 14, 2017
Messages
18,258
I didn't scroll your code across so hadn't realised you had already added 3 fields
Unfortunately its utterly confused
The first two fields are being set to the same control value
The first and third fields are identical but being set to different values

I've added the delimiters part is in RED below

you also need a space before WHERE

Code:
strSQL = "UPDATE tblUserSecurity SET [COLOR="Blue"]tblUsersecurity![strpassword]=Forms!frmEmpResetPassword![txtConfirmPassword][/COLOR], 
 tblUserSecurity![LoginID] = [COLOR="blue"]Forms!frmEmpResetPassword![txtConfirmPassword] [/COLOR], [COLOR="blue"]tblUserSecurity![strpassword][/COLOR] = Forms!frmEmpresetPassword![strpassword]" _
& " WHERE tblUserSecurity![LoginID] =[COLOR="DarkRed"] '" & [/COLOR]Forms!frmEmpResetPassword![txtRsetLoginEmp][COLOR="darkred"] & "'"[/COLOR]

The problem you have with trying to add complex code of this nature to your database is that you don't seem to have yet fully grasped basic concepts. That was why I kept trying to encourage you to at least look at or actually use existing code. Build your knowledge in small steps before attempting something else new

Recommend you check through everything that has been made to work so far.
Make sure you understand it completely before adding additional layers of complexity.

Good luck with the rest of your project
 

mba_110

Registered User.
Local time
Today, 15:59
Joined
Jan 20, 2015
Messages
280
Thanks everybody i have spotted the error and same time learn how to make our life easy with powerful SQL.

My working code is below.

Code:
strSQL = "UPDATE tblUserSecurity SET tblUserSecurity.LoginID = [Forms]![frmEmpResetPassword]![txtRsetLoginEmp], tblUserSecurity.StrPassword = [Forms]![frmEmpResetPassword]![txtConfirmPassword], tblUserSecurity.PED = [forms]![frmEmpResetPassword]![txtPasswordExpiryDays],tblUserSecurity.ResetDate = [forms]![frmEmpResetPassword]![txtResetDate]" _
& " WHERE tblUserSecurity![LoginID]= '" & Forms!frmEmpResetPassword![txtRsetLoginEmp] & "'"

Many thanks for your kind help, will see you shortly with another query.
 

Users who are viewing this thread

Top Bottom