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'")
Related
I am trying to use DLookup to check a text field against a database value except it returns an error when I try to do so. MemberID is the username and is being found using txtUsername field and the password is obviously password which are both retrieved from the Member table.
Here is the code:
Private Sub btnLogin_Click()
If Me.txtPassword.Value = DLookup("[Password]", "Member", "MemberID =
Me.txtUsername.Value") Then
MsgBox "Access Granted", vbInformation, "CD Shop"
MsgBox "Welcome", vbInformation, "CD Shop"
DoCmd.Close
DoCmd.OpenForm "frmGymActivity"
Else
MsgBox "Please re-enter your Username and Password."
End If
End Sub
You need to use proper quoting and string concatenation, to make sure you're passing a string with the value you want.
If Me.txtPassword.Value = DLookup("[Password]", "Member", "MemberID = " & Me.txtUsername.Value) Then
I am assuming MemberID is a number.
I'm a beginner using MS Access 2016.
I have a save button on a form that does some last minute input validation that is meant to be very direct and helpful to the end-user. I'm using a series of textboxes to step through the validation when they click the save button, this way they can complete then entire form and if there is an error it takes them directly to that field with the error.
My code appears to work,
it detects an error,
sets the focus to the text179,
sees there is an error in the JobNo field,
gives a message saying "Please enter a 5 digit job number"and
sets the focus to JobNo.
Then and only then, do I get a second prompt saying there is an error.
But why is it saying it can't set the focus to text179 when it clearly already has done so and should no longer be trying at that point?
Here is my code:
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" And Me.ItemNo & "" <> "" Then
Exit Sub
Else
Me.Text179.SetFocus
End If
End Sub
Private Sub Text179_GotFocus()
If Me.JobNo & "" Like "#####" Then
Me.Text181.SetFocus
Exit Sub
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Cancel = True
Me.JobNo.SetFocus
Exit Sub
End If
End Sub
Error:
Run-time error '2110':
Microsoft Access can't move the focus to the control Text179.
Let me know if any additional information is needed.
I was able to solve this with a much cleaner method, using just VB code without trying to use setfocus. It just took me a while to figure out how to nest my if statements correctly since I'm new with Access/VB. I'll post the code below.
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" Then
GoTo Eval_ItemNo
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Me.JobNo.SetFocus
Exit Sub
End If
Eval_ItemNo:
If Me.ItemNo & "" <> "" Then
GoTo Eval_QtyFinished
Else
MsgBox "Please enter an Item Number", vbOKOnly
Me.ItemNo.SetFocus
Exit Sub
End If
Eval_QtyFinished:
If Me.QtyFinished & "" <> "" Then
GoTo Eval_WorkCenter
Else
MsgBox "Please enter a Qty Finished", vbOKOnly
Me.QtyFinished.SetFocus
Exit Sub
End If
Eval_WorkCenter:
If Me.WorkCenter & "" <> "" Then
Exit Sub
Else
MsgBox "Please enter a WorkCenter", vbOKOnly
Me.WorkCenter.SetFocus
End If
End Sub
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
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
Can someone please help me on this find specific record Edit through MS access Forms
I have Frmfind form where I have one filed "ticket#" is a input text box
another filed was button "find"
When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...
So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..
note: Ticket# is column in my table whcih it is primary to have the ticket#.
Code:
Option Compare Database
Private Sub find_Click()
If IsNull(Me.Text79) Or Me.Text79 = "" Then
MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
Me.Text79.SetFocus
Exit Sub
End If
If [Ticket#] = Me.Text79.Value Then
MsgBox "Record found"
DoCmd.Close
DoCmd.OpenForm "frmEdit"
Else
MsgBox "not matching record"
Me.Text79.SetFocus
End If
End Sub
Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub
You can use this code:
Private Sub Command112_Click()
Me.txtSeach.SetFocus
On Error Resume Next
DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"
End Sub
Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:
DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")
Then for your message, just place that code in the forms on-open event that has a cancel:
eg:
Private Sub Form_Open(Cancel As Integer)
If IsNull(Me!TicketNumberID) Then
MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
Cancel = True
End If
End Sub
The above assumes your search column is ticket number.
You can also use dlookup(), or even dcount(). I think above is less code, but:
Dim strWhere As String
strWhere = "TicketNumber = " & Me.text79
If DCount("*", "tblBookings", strWhere) > 0 Then
code for Record found goes here
Else
code reocrd not found code here
End If
So either way should suffice here.