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
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Password – Number of attempts (97/2k)
Hi Rob,
I don’t have time to work out an example for you now . 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.
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
Pat,
No it’s not. It’s a class module I got off the web from http://www.frez.co.uk/freecode.htm%5B/url%5D
I took the VB version and imported it into Access and everything was fine.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications