Arrays want to be iterated with For...Next
; Object collections want to be iterated with For Each...Next
. Using For Each
to iterate a large array is going to be slower than it needs to be.
Converting 10K strings to lowercase is also taking a toll on performance.
Using InStr
is verifying whether the password contains the forbidden password, not whether it is one of them: assuming you mean to check if the user's password exists in the forbidden passwords list, you don't need InStr
.
If you want to stick with an array, use For...Next
to iterate it, and specify Option Compare Text
to have the =
operator treat "string"
the same as "STRING"
(default is Option Compare Database
in Access, which matches your database settings - in all other hosts it's Option Compare Binary
, which is case-sensitive).
But you shouldn't need to iterate any array. Assuming the 10K strings are all unique, you can have them added to a keyed Collection
, and then you can instantly know if the user's password is in the collection by trying to add the user's password to the collection:
Private Function IsForbiddenPassword(ByVal Value As String) As Boolean
'assuming a ForbiddenPasswords global collection exists
On Error Resume Next
ForbiddenPasswords.Add Value, Value 'if the key already exists, an error is raised
If Err.Number <> 0 Then
'key already exists: password is forbidden
IsForbiddenPassword = True
Else
'.Add was successful, password is not forbidden (gotta remove it now!)
ForbiddenPasswords.Remove Value
End If
On Error GoTo 0
End Function
Alternatively, use a Scripting.Dictionary
and its Exists
method, which returns True
if a given key already exists:
Private Sub txtAdminPassword_Change()
'where ForbiddenPasswords is a Dictionary instance keyed with the passwords:
lblInsecureWarning.Visible = ForbiddenPasswords.Exists(Value)
End Sub
SELECT * FROM tblBanned WHERE PWD='" & txtAdminPassword.Text & "'"
) and see if it returns any records.