1

I need to check if values (approx 10.000 insecure passwords) stored in a one-dimensional array occur in a TextBox and display a warning if necessary.

But due to the constant checking after each keystroke the input into the TextBox is very delayed.

Does anyone know a faster way to do this?

Greetings Ronny

'ForbiddenPasswords = one-dimensional array 
Private Sub txtAdminPassword_Change()
Dim VarDat As Variant

For Each VarDat In ForbiddenPasswords
    If InStr(1, LCase(txtAdminPassword.Text), VarDat) > 0 Then
        lblInsecureWarning.Visible = True
        Exit For
    End If
Next
End Sub
7
  • 1
    How about checking at the BeforeUpdate event only?
    – Gustav
    Commented Mar 23, 2021 at 16:13
  • Being a password, I would say it should be identic with an array element. Case sensitive, too. Am I wrong? If not, it can be checked very fast...
    – FaneDuru
    Commented Mar 23, 2021 at 16:16
  • Are all the passwords in the array low case? Do you really want checking if the text box text exists inside an array element, or if it (in low case) exists in the array?
    – FaneDuru
    Commented Mar 23, 2021 at 16:32
  • 4
    If the list of banned passwords is static, then they should be stored in a table. Therefore, rather than populating and checking an array, just use a recordset (SELECT * FROM tblBanned WHERE PWD='" & txtAdminPassword.Text & "'") and see if it returns any records.
    – Applecore
    Commented Mar 23, 2021 at 17:05
  • Actually, @Applecore, having a dynamic list points to storing them in a database table, too!
    – FreeMan
    Commented Mar 23, 2021 at 17:10

4 Answers 4

3

Rather than loading the list of banned passwords in an array and searching through it to find if it exists, consider using a table to store the (lower case) passwords. You can then open a recordset based on the text entered, and see if it returns any records. Something like:

Function fCheckBannedPWD(strPWD As String) As Boolean
'   returns true if the entered password is banned, otherwise false
    On Error GoTo E_Handle
    Dim db As DAO.Database
    Dim rsPWD As DAO.Recordset
    Dim strSQL As String
    Set db = CurrentDb
    strSQL = "SELECT BannedPWD FROM tblPWD WHERE InStr('" & LCase(strPWD) & "',BannedPWD)>0;"
    Set rsPWD = db.OpenRecordset(strSQL)
    If Not (rsPWD.BOF And rsPWD.EOF) Then
        fCheckBannedPWD = True
    End If
fExit:
    On Error Resume Next
    rsPWD.Close
    Set rsPWD = Nothing
    Set db = Nothing
    Exit Function
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "fCheckBannedPWD", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume fExit
End Function

Regards,

8
  • Upvoted! I think this would be the idiomatic way in Access... yet I can't help but wonder about the concatenated user input (especially since a password could include some legitimate weird characters not normally seen in user input)... Access OM should have a way to easily parameterize queries. Commented Mar 23, 2021 at 17:40
  • 2
    Yeah, good old Bobby Tables!! I agree that what I've suggested is not perfect, but it is a starting point, and it should offer much greater performance than the OP was originally experiencing.
    – Applecore
    Commented Mar 23, 2021 at 17:51
  • 1
    Please excuse that I was a bit unspecific at first. I want to check if certain unsafe strings and words are in the password that is being entered into the TextBox. Everything is compared with lowercase letters, because unsafe characters can follow lowercase and uppercase. Here are a few examples: | Textbox value = srlkjn3we45th0pdragonfn45n | array value = dragon | Match = True | textbox value = /§$LHKJFDOZU$?dlkjnhOusE4iohu | Array value = house | Match = True | Commented Mar 24, 2021 at 7:02
  • 1
    Code has been modified to use InStr to check for the existence of the banned words in the given string.
    – Applecore
    Commented Mar 24, 2021 at 9:04
  • @CookieKiller the more I read what you're trying to do, the more it sounds like an X-Y problem... what is this "password" being used for anyway? Application-level security implemented in VBA is essentially security theater for the most part, and passwords should be salted and hashed (note: hashed, not encrypted) and never stored in plain-text. I'm worrying that you're doing all this work for what amounts to very little actual value if this password scheme means to actually secure anything that actually needs security. That said having this example data in the OP would have been great! Commented Mar 24, 2021 at 13:06
2

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
4
  • 2 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. I did not know that, thank you for the information. :-) Commented Mar 24, 2021 at 7:00
  • I have now tried the following, but still damn slow. For i = LBound(ForbiddenPasswords) To UBound(ForbiddenPasswords) If InStr(1, LCase(txtAdminPassword.Text), ForbiddenPasswords(i)) > 0 Then lblInsecureWarning.Visible = True Exit For End If Next i Commented Mar 24, 2021 at 7:07
  • This can be speeded up by using variables to store the lower and upper bounds of the array, rather than calculating them each time as part of the loop - intLower=LBound(ForbiddenPasswords): intUpper=UBound(ForbiddenPasswords): For i=intLower To intUpper
    – Applecore
    Commented Mar 24, 2021 at 9:06
  • @Applecore As far as I know the start and final values of a For statement are evaluated only once, at the start of the For loop. They are not evaluated at each loop iteration. Commented Mar 26, 2021 at 9:22
1

Try the next way, please:

  1. Create a Private variable on top of the module (in the declarations area):
Private objExcel As Object
  1. Copy the next function code:
Function passExists(strPass As String, arr) As Boolean
   Dim lPath As String
   lPath = UCase(strPass)
   passExists = Not IsError(objExcel.Application.match(lPath, arr, 0))
End Function
  1. It can be checked in the next way:
Sub checkPasswordInArray()
 Dim ForbiddenPasswords, boolMatch As Boolean, strPass As String
   If objExcel Is Nothing Then Set objExcel = CreateObject("Excel.Application")
   ForbiddenPasswords = Split("pass1,pass2,pass3", ",")
   strPass = "Pass2" 'txtAdminPassword.Text
   Debug.Print passExists(strPass, ForbiddenPasswords)
End Sub

In order to be very fast, objExcel must not be created each time you need to use the function, but you must take care to quit after using it...

2
  • 1
    That would work very nicely in Excel, but should an Access project reference the Excel OM and spawn an Excel.Application instance for this? Commented Mar 23, 2021 at 17:10
  • 1
    @Mathieu Guindon: Ups... I missed the Access tag. I will adapt the above code to firstly create the Excel Object, in order to use it. Thanks!
    – FaneDuru
    Commented Mar 23, 2021 at 17:26
0

Just to be complete, 2 additions to the propositions found.

  1. txtAdminPassword.Text LIKE vardat & "*" is about 6x faster than InStr(1, LCase(txtAdminPassword.Text), VarDat) > 0

  2. for even better perf with your array, you can use Filter:

    Dim strSubNames As Variant
    strSubNames = Filter(forbiddenPasswords, txtAdminPassword.Text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.