I have a user login form on my access database with a table of users and other user criteria. One is a checkbox that if "true" will open up a change password form when the following code is ran. But when this checkbox field returns true, access 2010 crashes. So far, Google has left me empty handed. Any ideas on what would be causing the crash? Is it something in the code or could it be an issue with the "change password" form? The form opens just fine when I manually open it by "double clicking" it.
'Check if password needs to be reset
If Me.cboUser.Column(3) = True Then
DoCmd.OpenForm "frmPasswordChange", , , "UserID = " & Me.cboUser
End If
The entire code is this:
Private Sub txtPassword_AfterUpdate()
'Check that EE is selected
If IsNull(Me.cboUser) Then
MsgBox "You need to select a user!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(3) = True Then
DoCmd.OpenForm "frmPasswordChange", , , "UserID = " & Me.cboUser
End If
DoCmd.OpenForm "Opening Screen"
Me.Visible = False
DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblAuditTrail ([DateTime], UserName, Action) VALUES (Now(),'" & Me.cboUser.Column(1) & "','Login')")
DoCmd.SetWarnings True
Else
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
End Sub
Related
Sorry to bother you all again...
I am trying to create a login page where upon inputting the correct UserName AND password, it would filter a subform below so that they can see only their comments. I have it so that there are two buttons where one checks the Username and Password via VBA code and another button filters using a Macro... But I would like for one button to do everything so that if the password is incorrect, then they cannot see anything. The filter button currently works without a password and as long as there is a correct username it will filter that without looking at the password.
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
Dim search_text As String
search_text = Me.txtUserName
If Nz(Me.txtUserName.Value, "") = "" Then
Me.FilterOn = False
Me.txtUserName.SetFocus
Exit Sub
End If
Me.Filter = "UserName like '*" & Me.txtUserName.Value & "*' or userName like '*"
Me.FilterOn = True
Me.txtUserName.SetFocus
Me.txtUserName.Value = search_text
Me.txtUserName.SelStart = Len(Nz(Me.txtUserName. Value, "")) & 1
End Sub
I decided to just make a macro and do a docmd to run the macro at the end of the code :) thank you all
When a user has logged in i would like it so that their user name is displayed in a Label in the main menu. here is my code.this at the minute allows the login form to check details entered and compares them to the employees table to allow access to the Home form.
Option Compare Database
Option Explicit
Private Sub ButtonLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TxtUsername & "'"
If rs.NoMatch Then
Me.LblWronguser.Visible = True
Me.TxtUsername.SetFocus
Exit Sub
End If
Me.LblWronguser.Visible = False
If rs!Password <> Nz(Me.Txtpassword, "") Then
Me.LblWrongpass.Visible = True
Me.Txtpassword.SetFocus
Exit Sub
End If
Me.LblWrongpass.Visible = False
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name
End Sub
after this stage i am stuck as to what the code for the "home" form to display the user that has just logged in (label) this would be the Unbound field seen.
Assuming table structure is similar to below and the label is actually textbox you can use a Dlookup function.
For this scenario, I am going to assume your textbox is called Welcome. You'll probably need to use something like this:
Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.TxtUsername & "'"
If rs.NoMatch Then
Me.LblWronguser.Visible = True
Me.TxtUsername.SetFocus
Exit Sub
Else
Me.Welcome = rs!UserName
End If
Me.LblWronguser.Visible = False
If rs!Password <> Nz(Me.Txtpassword, "") Then
Me.LblWrongpass.Visible = True
Me.Txtpassword.SetFocus
Exit Sub
End If
Me.LblWrongpass.Visible = False
TempVars("EmployeeType") = rs!EmployeeType_ID.Value
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name
Note, this would only apply the username to the Welcome textbox. If a name was required, you would need to reference such a column differently, such as Me.Welcome = rs!FirstName or something. Also, since there is a password verification, you probably want to restructure things so that the name would only populate after the password portion has been passed properly.
'Check to see if the recordset actually contains rows
If Not (recordSet.EOF And recordSet.BOF) Then
recordSet.MoveFirst 'Unnecessary in this case, but still a good habit
'See if credentials match data
Do
If (recordSet!User_ID = PERSAL And recordSet!Password = Password) Then
'Open Menu form
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
'Close Login form
DoCmd.Close acForm, "Login"
Exit Do
End If
recordSet.MoveNext
If (recordSet.EOF Or recordSet.BOF) Then
MsgBox "Your credentials are incorrect or you are not registered."
Exit Do
End If
Loop
'Match the values entered for PERSAL nr. and password fields with a row in User table
Else
MsgBox "There are no records in the recordset."
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
End If
Form_Login.txtUser_ID.SetFocus
I have tried every solution I've found for the past 2 days but nothing has worked to solved my error 2501:
I did:
Decompiling
Compact and repair
Tackling the MSCOMCTL.OCX file
Importing to a new access file
I am spent.
For the MSCOMCTL.OCX file I'm thinking to download a 2007 Office update SP 3.
The exception/error appears on this line:
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal
Your syntax is not correct. Try:
DoCmd.OpenForm "Menu", acNormal, , , , acWindowNormal
I have two fields, username and password, currently there are two users, my code only accepts the 1st user's username and password, but not the second user. Cant seem to figure out where i should make the changes. I get an error in ms access related to duplicate data entry.
Please tell me what i can do to change my code, currently my visual basic code logic is, if username and password are entered correctly then open a form
Private Sub LoginBtn_Click()
Set L = CurrentDb.OpenRecordset("Login")
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUsername.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
If (Me.txtUsername = L.[Username]) And (Me.strPassword = L.[Password]) Then
'Close Member login form and open the member form
DoCmd.OpenForm "Memberform"
Else
MsgBox "Password Invalid. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
End If
End Sub
It looks like the problem is the last If block: you're opening the Login table (or query?) but you're only looking at the first record, instead of looping through all the records to check if there's a match.
So, you could fix this by changing it to loop through the records until you find a match (not recommended), or you could instead make a query whose criteria are the supplied username and password and see if any records are returned.
Way 1 - looping through records:
Private Sub LoginBtn_Click()
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUserName) Or Me.txtUserName = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUserName.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
Set L = CurrentDb.OpenRecordset("Login")
Dim found As Boolean
found = False
If L.BOF And L.EOF Then
MsgBox "There are no usernames or passwords in the database.", vbOKOnly, "No login info"
Exit Sub
Else
Do While Not L.EOF
If (Me.txtUserName = L.[UserName]) And (Me.strPassword = L.[Password]) Then
found = True
Exit Do
End If
L.MoveNext
Loop
End If
If L.EOF And Not found Then
' at the last record and did not find a match
MsgBox "Invalid username or password. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
' clean up
L.Close
Set L = Nothing
Else
' we found a match; close Member login form and open the member form
' clean up
L.Close
Set L = Nothing
DoCmd.OpenForm "Memberform"
DoCmd.Close acForm, Me.Name
End If
End Sub
Way 2 - a query:
Private Sub LoginBtn_Click()
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUsername.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
' Using a QueryDef with parameters to avoid SQL injection
Dim qdf as DAO.QueryDef
Set qdf = CurrentDb.CreateQueryDef("","SELECT Username, Password FROM Login WHERE Username=[_uname] AND Password=[_pw]")
qdf.Parameters("_uname") = Me.txtUsername
qdf.Parameters("_pw") = Me.strPassword
Set L = qdf.OpenRecordset
If L.BOF And L.EOF Then
' no match
MsgBox "Invalid username or password. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
' clean up
L.Close
Set L = Nothing
Else
' we found a match; close Member login form and open the member form
'clean up
L.Close
Set L = Nothing
DoCmd.OpenForm "Memberform"
DoCmd.Close acForm, Me.Name
End If
End Sub
This is my first attempt to create a login form. I've read up a few forums about it and tried them myself. However, I've encountered the error when trying out the form.
"Run time error '2001': You canceled the previous operation."
Here's my code! The error highlighted is the DLOOKUP statement. When I move my cursor to LanID, it appears to be 0. (I guess it got something to do with it?)
Option Compare Database
Option Explicit
Private intLoginAttempts As Integer
Private Sub cmdLogin_Click()
'Check mandatory fields
If IsNull(Me.txtLanID) Or Me.txtLanID = "" Then
MsgBox "Lan ID is required.", vbOKOnly, "Required Data"
Me.txtLanID.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "Password is required.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Compare input password and database password
If Me.txtLanID <> "" Then
If Me.txtPassword = DLookup("Password", "tblUser", "[LanID]=" & Me.txtLanID) Then
LanID = Me.txtLanID
'Close Login Page
DoCmd.Close acForm, "frmUserLogin", acSaveNo
'Check whether user is an admin
If Me.txtAdmin = DLookup("Administrator", "tblUser", "[LanID]=" & Me.txtLanID) Then
If Me.txtAdmin = -1 Then
DoCmd.OpenForm "frmMenu"
Else
DoCmd.OpenForm "frmBack"
End If
End If
Else
MsgBox "Invalid Password. Try again.", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If
End If
'If user enters incorrect password 3 times
intLoginAttempts = intLoginAttempts + 1
If intLoginAttempts = 3 Then
MsgBox "You do not have access to the database. Please contact system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
If tblUser.LanID is text data type, enclose Me.txtLanID in quotes to avoid an error with your DLookup() expression. (You don't need .Value there because it's the default property.)
DLookup("Password", "tblUser", "[LanID]='" & Me.txtLanID & "'")
If that was not the explanation, test your DLookup() expression in the Immediate window (go there with Ctrl+g) to see whether it throws an error or what value it does return.
Note, when you test DLookup() in the Immediate window, use the current value of Me.txtLanID. For example, if Me.txtLanID contains the text "foo", test it like this ...
? DLookup("Password", "tblUser", "[LanID]='foo'")