I am attempting to create a user login form in access '07. The code that I have so far is not erroring out, however, it is not doing what I want it to do. It is a relatively simple login form. The user opens the db and a login form is loaded. The user will select their username from the combo box and then focus is placed on the password field. The user enters in an assigned pwd given to them by the administrator. When the user clicks on login, the code should compare the pwd string in the textbox to the adminassigned pwd on the users table. The code is not doing that and it is not erroring out. The 'Users' table has the following columns: username, password, assignedpassword and confirm password. Can someone take a look at my code and tell me what I am missing? Thanks!!
Option Compare Database
Const NumLoginAttempts = 3
Dim Attempts As Integer
Private Sub Cbousername_AfterUpdate()
'If a username is selected, then place the focus on the password field.
If Not IsNull(Me.Cbousername) Then
Me.CmdLogin.Enabled = True
Private Sub CmdLogin_Click()
'Check to make sure that there are characters in the txtpwd field
'If not, display message telling user that a password must be entered and place focus back on the password box
If IsNull(Me.txtPwd) Then
MsgBox "You must enter a password.", vbExclamation + vbOnly, "Required Entry"
'Validate password for user
If Me.txtPwd.Value = DLookup("Password", "Users", "[ID]=" & Me.Cbousername.Value) Then
ID = Me.Cbousername.Value
'Close the login form and open frmMain
DoCmd.Close acForm, "FrmLogin", acSaveNo
If Me.txtPwd.Value <> DLookup("Password", "Users", "[ID]=" & Me.Cbousername.Value) Then
MsgBox "The password you entered does not match the username selected.", vbExclamation + vbOnly, "Try Again"
Me.txtPwd = ""
Attempts = Attempts + 1
'After 3 tries, the db will close
If Attempts > 3 Then
MsgBox "Maximum number of attempts reached. Please contact administrator to reset your password.", vbExclamation + vbOKOnly, "Database Shutdown"
'Compare txtpwd field value to the assignedpassword value for the user that is logging in
'If the password that the user types in matches the assignedpassword in tblusers, then have the user create a new password
If Me.txtPwd.Value = DLookup("AssignedPassword", "Users", "[ID]=" & Me.Cbousername & " Or [Password] Is null") Then
MsgBox "You must enter a new password.", vbExclamation + vbOKOnly, "Invalid Password"
Me.LblPwd.Visible = False
Me.txtPwd.Visible = False
Me.LblNewPwd.Visible = True
Me.TxtNewPwd.Visible = True