Case sensitive password validation (1 Viewer)

donsi

Registered User.
Local time
Today, 03:10
Joined
Sep 1, 2016
Messages
73
Hello everyone,

I am sure this may be just a simple thing and may have been asked before. I found some solution to DLookup but I am not currently using DLookup so need to find another way. Below is the code I have behind BtnLogin to validate username and password. Right now it is not comparing for case sensitive when validating password. How can I achieve that.

here is the code

Code:
Private Sub BtnLogin_Click()
    Dim rs As Recordset
    On Error GoTo Problem

'Check to see if data is entered into Username
 
    If IsNull(Me.TxtUserID) Then
        MsgBox "Please enter a UserID.", vbOKOnly, "Required Data"
            Me.TxtUserID.SetFocus
        Exit Sub
    End If

'Check to see if data is entered into Password

    If IsNull(Me.TxtPassword) Then
        MsgBox "You must provide a password.", vbOKOnly, "Required Data"
            Me.TxtPassword.SetFocus
        Exit Sub
    End If
    


'Lookup correct password for username entered in Username

    Set rs = CurrentDb.OpenRecordset("TblUsers", dbOpenSnapshot, dbReadOnly)
    rs.FindFirst "UserId='" & Me.TxtUserID & "'"
    
    If rs.NoMatch = True Then
        MsgBox "Incorrect User Name."
          Me.TxtUserID.Value = ""
          Me.TxtUserID.SetFocus
        Exit Sub

    End If
    
    If rs!Password <> Nz(Me.TxtPassword, "") Then
        MsgBox "Incorrect Password."
          Me.TxtPassword.Value = ""
          Me.TxtPassword.SetFocus
        Exit Sub
    End If
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:10
Joined
Jan 20, 2009
Messages
12,849
Use the StrComp() function and set the Compare parameter to vbBinaryCompare

BTW Revealing if the password or username is the issue weakens the system.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:10
Joined
May 7, 2009
Messages
19,169
On your module header put:

Option Compare Binary
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:10
Joined
Jan 20, 2009
Messages
12,849
I had to remove Option compare Database though for it work as I was receiving an error.

Of course. You can only have one of the Option Compare settings. (Binary, Text or Database).

Be aware that the setting affects all comparisons in the module.
 

isladogs

MVP / VIP
Local time
Today, 10:10
Joined
Jan 14, 2017
Messages
18,186
Of course. You can only have one of the Option Compare settings. (Binary, Text or Database).

Be aware that the setting affects all comparisons in the module.

Whilst I understand that completely, on the occasions I've used StrComp and vbBinaryCompare, I've used Option Compare Database as 'normal':

For example:
Code:
 'exclude items that give invalid results
        'exclude 3 letter upper case results like currency values (but allow DOB & URL)
        'NOTE: if the 2 items being compared are equal, the result  = 0
        If strFieldName <> "DOB" And strFieldName <> "URL" Then
            If Len(strFieldName) = 3 And StrComp(UCase(strFieldName), strFieldName, vbBinaryCompare) = 0 Then GoTo NextItem
        End If

Also this code written by Chip Pearson:
Code:
If StrComp(Right(FolderName, 1), "\", vbBinaryCompare) = 0 Then
        FName = FolderName & FName
    Else
        FName = FolderName & "\" & FName
    End If

In all such cases, the code works fine.
And in my ignorance, I'd never thought about doing it any other way.
What advantages are there in using Option Compare Binary instead?
And if this isn't a totally stupid question, what effect would this have on the rest of the code in that module?
 

missinglinq

AWF VIP
Local time
Today, 06:10
Joined
Jun 20, 2003
Messages
6,423
....what effect would this have on the rest of the code in that module...
All comparisons in that module would be Case Sensitive, which may or may not be desirable/acceptable.

For example, most apps require that a password be Case Sensitive (hence increasing security by increasing the number of possible combinations) but not a user name...so using

Option Compare Binary

would be a problem, while using StrComp() with the Compare parameter to vbBinaryCompare for the password alone wouldn't be.

Linq ;0)>
 

isladogs

MVP / VIP
Local time
Today, 10:10
Joined
Jan 14, 2017
Messages
18,186
Hi linq
That confirms what I thought.
Therefore I was correct in not needing Option Compare Binary as well as vbBinaryCompare in the code I supplied.

Galaxiom
Could you explain to me why you suggested using both of these in post 2. Thanks
 

Users who are viewing this thread

Top Bottom