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.
Related
Ok So here is my code. I think I'm close but when the frmCondition/Concerns Update form opens it ask for the strCBOProperty value. I know it is probably a syntax error, but I don't know what it is.
Private Sub btnLogin_Click()
Dim strCBOPassword As String
Dim strPassword As String
Dim strCBOProperty As String
Selectnull
strCBOProperty = Me.cboProperty.Column(0)
strCBOPassword = Me.cboProperty.Column(1)
strPassword = Me.txtPassword
If strCBOPassword = strPassword Then
MsgBox "Login Successful!"
DoCmd.BrowseTo acBrowseToForm, "frmCondition/Concerns Update", , "[Forms]![frmCondition/Concerns Update]!cbopropertyname = strCBOProperty"
Else
MsgBox "Invalid Password"
End If
End Sub
Private Sub Selectnull()
If IsNull(Me.cboProperty) Then
MsgBox "Please select a property", vbOKOnly
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter a password", vbOKOnly
End If
End Sub
I'd say you need something like this:
DoCmd.OpenForm "frmCondition", WhereCondition:="PropertyName='" & Me.cboProperty.Value & "'"
If not, tell us more about the contents of cboProperty and the data type of the property column in the conditions table.
If you have the value in a variable:
DoCmd.OpenForm "frmCondition", WhereCondition:="PropertyName='" & strCBOProperty & "'"
The variable must be outside of the string.
Using named parameters makes sure you get the parameter right. You have it on the FilterName position.
I am trying to run the following code, but I'm getting a Run-time error '2471' on DLookUp. "The expression you entered as a query parameter produced this error: 'dkim'".
DLookUp returns a value, but it's returning the wrong value. This code is supposed to compare Text7 to a dlookup 'Password' value. Am I missing something?
Private Sub btn_enter_Click()
If Me.Text7.Value = DLookup("Password", "tbl_ref_users", _
"[ID]=" & Me.Text5.Value) Then
ID = Me.Text5.Value
'Close logon form and open splash screen
DoCmd.Close acForm, "form_Login", acSaveNo
DoCmd.OpenForm "form_Menu"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.Text7.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
I'm getting a Run-time error '2471' on DLookUp. "The expression you
entered as a query parameter produced this error: 'dkim'".
Since the DLookup expression is DLookup("Password", "tbl_ref_users", "[ID]=" & Me.Text5.Value), perhaps the .Value of Me.Text5 is dkim.
In that case, quote the text box value in the DLookup criteria:
DLookup("[Password]", "tbl_ref_users", "[ID]='" & Me.Text5.Value & "'")
However the datatype of tbl_ref_users.ID must be text in order for that to work. If it's numeric, you need to compare it to a number instead of a text string such as "dkim".
Also note I bracketed the field name Password because it's a reserved word.
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'")
I've looked around for ages and I haven't found an answer to my problem so I was hoping someone here could help me.
I am creating a system using Microsoft Access where I have a members table containing a username and password and various other fields such as date of birth, etc.
I want to create a form where users can enter a username and password. By clicking a button on this form, these details will then be checked against the usernames and passwords in the members table. If the details match, a message will be displayed saying they have logged in. If the details are not found in the table, a message saying the details are incorrect will show.
How can I do this without using VBA?
I have started by creating a form called loginform with two text boxes loginusername and loginpassword.
Where should I go from here?
The VBA solution shouldn't be that complicated. A quick and dirty solution:
Dim Result as Variant
Result=Dlookup("Password","tblMembers","UserName='" & nz(loginusername.value,"") & "'")
If nz(Result,"")<>nz([login password].value,"") Then
MsgBox "Invalid password"
Else
MsgBox "Password correct"
End If
'set the variables
Dim UN As String
Dim PW As String
Dim user, pass As Boolean
'make sure none of the fields are null, or blank
UN = Text
PW = Text
If IsNull(Username) Then
MsgBox "You must enter a username."
Username.SetFocus
Else
'assign true to user
user = True
End If
If IsNull(Password) Then
MsgBox "You must enter a password."
Password.SetFocus
Else
pass = True
End If
If user = True And pass = True Then
UN = DLookup("[Username]", "LoginTable", "[Username]= '" & Me.Username & "'")
PW = DLookup("[Password]", "LoginTable", "[Password] = '" & Me.Password & "'")
End If
If Me.DummyUser = Me.Username And Me.DummyPass = Me.Password Then
MsgBox "Access granted."
Else
MsgBox "Access denied."
End If