• Password – Number of attempts (97/2k)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Password – Number of attempts (97/2k)

    Author
    Topic
    #379489

    I am trying to figure out the code which would allow a user three attempts at a password and if still incorrect, an action is invoked.
    The textbox requiring the password is on a form. It has no buttons as yet.
    Help please.

    Rob

    Viewing 1 reply thread
    Author
    Replies
    • #632139

      Hi Rob,

      I don’t have time to work out an example for you now sorry. Have a look at ACC2000: How to Create a Password Protected Form or Report. It shouldn’t be too hard to add a variable intNumberOfAttempts to the sample code given there, increment it each time a password is evaluated and do whatever you deem necessary when a predetermined count is reached.

      • #632335

        Thanks Hans. I’ve downloaded the code and I’m working my way through it.

        Regards

        Rob

    • #632302

      Rob,

      Here is the code I use. You will notice a CMD5 Type, that is an MD5 Hashing Class module. I store my passwords using the MD5 hashing algorythm in the db and not plaintext. MD5 is a one way algorythm.

      The key component is the Static declaration for intTries. Static keeps the value across separate “runs” of the code.

      A couple of other notes:

      gcintLOGINATTEMPTS is a global constant that defines the number of login attempts since I needed a way to quickly change the number of attempts. IIRC, if you set this value to a negative number, then you can an unlimited number of attempts.

      Private Sub cmdOK_Click()
      '--------------------------------------------------------------------------
      '.Purpose      : To handle the OK button Click event and
      '.                verify login information
      '.Author       : Bryan Carbonnell
      '.Date         : 18-Oct-2002
      '.Revised      : 18-Oct-2002 - Original
      '--------------------------------------------------------------------------
      Const cstrProcName As String = "cmdOK_Click"
      Static intTries As Integer
      Dim strSQL As String
      Dim rst As DAO.Recordset
      Dim md5 As CMD5
      Dim strMsg As String
      
      'Build SQL String
      strSQL = "SELECT * FROM tblUser " & _
                "WHERE UserName ='" & txtUserName & "'"
      'Open RecordSet
      Set rst = CurrentDb().OpenRecordset(strSQL)
      
      If rst.BOF = True And rst.EOF = True Then
        'No records, so we have an invalid UserName
        intTries = intTries + 1
        mbolValidLogin = False
      Else
        'Valid UserName, now we need to check the password
        Set md5 = New CMD5
        If md5.md5(txtPassword & "")  rst!fldPassword Then
          'Invalid Password
          intTries = intTries + 1
          mbolValidLogin = False
        Else
          mbolValidLogin = True
          gstrUserName = txtUserName
        End If
      End If
      
      If intTries >= gcintLOGINATTEMPTS  And gcintLOGINATTEMPTS > 0Then
        'Too many tries, so boot the user
        strMsg = "You have exceeded the maximum number of Login attempts." _
                  & vbCrLf & "" _
                  & vbCrLf & "Please contact the Systems Administrator if you continue" _
                  & vbCrLf & "having problems logging in." _
                  & vbCrLf & "" _
                  & vbCrLf & "The application will now shut down."
        'Display message box
        MsgBox strMsg, vbCritical + vbOKOnly, "Login Failure"
        '
        Application.Quit
      Else
        Select Case Me.ValidLogin
          Case False
            'Invalid Login
            strMsg = "Invalid User Name or Password entered." & vbCrLf & vbCrLf & _
                      "Click OK to try again. Clcik Cancel to Exit Application."
            
            If MsgBox(strMsg, vbOKCancel + vbExclamation, "Invalid Login") = vbCancel Then
              Application.Quit
            End If
          Case True
            'Valid Login
            Me.Visible = False
        End Select
      End If
      End Sub
      • #632336

        Thanks Bryan. I’m trying to get my head around two sets of code. I’m still a relative beginner wrt Access. The exercise will do me good. I’ll get back if I experience difficulties.

        Best Wishes

        Rob

    Viewing 1 reply thread
    Reply To: Reply #632335 in Password – Number of attempts (97/2k)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel