I have a problem with Microsoft access for code I have written to lookup the USER TYPE field in the ADMINSTRATOR TABLE.It is not able to run due to syntax error 3075 missing expression operator in query expression 'USER TYPE'
It is used for my login menu to differentiate between two different user types and bring them to different forms.
The code works before I implemented the User Security code
Dim UserLevel As Integer
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
Here is my code:
Private Sub Command1_Click()
Dim UserLevel As Integer
If IsNull(Me.txtLoginID) Then
MsgBox "Please enter Login ID", vbInformation, "Login ID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("[User ID]", "ADMINSTRATOR TABLE", "[User ID]='" & Me.txtLoginID.Value & "' And Password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect Password or Login ID", vbInformation, "Login unsuccessful"
Else
UserLevel = DLookup("USER TYPE", "ADMINSTRATOR TABLE", "USER ID = '" & Me.txtLoginID & "'")
DoCmd.Close
If UserLevel = 1 Then
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "MAIN MENU"
Else
MsgBox "Login Success", vbInformation, "Login Success"
DoCmd.OpenForm "ADMINISTRATOR FORM"
End If
End If
End If
End Sub
Field names and table names containing spaces or special characters (poor design choice) need to surrounded with square brackets.
Related
i used this code to protect my log in, but i had a problem with the password, for example i've two username and two password...when i used Username both of password can log in...can anyone help...here the code i used...
Private Sub cmdmsk_Click()
Dim UserLevel As Integer
Me.cmdmsk.SetFocus
If IsNull(Me.txtuser) Then
MsgBox "plis enter username", vbInformation, "Username needs to Login"
Me.txtuser.SetFocus
ElseIf IsNull(Me.txtpass) Then
MsgBox "plis enter you password", vbInformation, "Password needs to login"
Me.txtpass.SetFocus
Else
'process the job
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin ='" & Me.txtuser.Value & "'"))) Or _
(IsNull(DLookup("Password", "tblUser", "Password ='" & Me.txtpass.Value & "'"))) Then
MsgBox "wrong pass or username"
Else
UserLevel = DLookup("UserSecurity", "tblUser", "UserLogin = '" & Me.txtuser.Value & "'")
If UserLevel = "1" Then
MsgBox "Cangratulations ^_^"
DoCmd.Close
DoCmd.OpenForm "MENU"
Else
DoCmd.Close
DoCmd.OpenForm "INPUT"
End If
End If
End If
End Sub
Do one look up only:
Private Sub cmdmsk_Click()
Dim UserLevel As Variant
Me!cmdmsk.SetFocus
If IsNull(Me!txtuser.Value) Then
MsgBox "Please enter your username.", vbInformation, "Missing Username"
Me!txtuser.SetFocus
ElseIf IsNull(Me!txtpass.Value) Then
MsgBox "Please enter your password", vbInformation, "Missing Password"
Me!txtpass.SetFocus
Else
'process the job
UserLevel = DLookup("UserSecurity", "tblUser", _
"[UserLogin] = '" & Me!txtuser.Value & "' And [Password] = '" & Me!txtpass.Value & "'")
If IsNull(UserLevel) Then
MsgBox "Incorrect username or password.", vbInformation, "Login"
Else
If UserLevel = "1" Then
DoCmd.OpenForm "MENU"
Else
DoCmd.OpenForm "INPUT"
End If
DoCmd.Close
End If
End If
End Sub
im using access 2007.
My login form has username and password and i want to pass value of the username from login form to menu form. my code as below
'Login Form'
Private Sub Command1_Click()
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
End If
End If
End Sub
i put the code for the menu form
Me.Label4 = "Hi " & Me.OpenArgs & "!"
i would like the menu form automatically detect what is the username. Example
Hi John!!
The error says invalid argument. qWhere is the error in my code?
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
OpenArgs is not the third argument, it is the seventh, which may account for the error (the third argument is a filter):
DoCmd.OpenForm "Menu", acNormal, , , , , "Bob"
(or use named arguments)
then
MsgBox "Hello " & Me.OpenArgs
will work.
You can just store the username as a variable in your login form and set the message right after you open the menu:
'Login Form'
Private Sub Command1_Click()
Dim un As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
un = Me.txtUserName
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
Forms!Menu!Label4 = "Hi " & un & "!"
End If
End If
End Sub
If it throws an error, move the DoCmd.Close to the end.
You have 2 possibilities.
call a Public sub in your menu form
In your Menu form, create the following sub :
Public Sub Pass_Login_Info(strUserName As String)
Me.Label4 = "Hi " & strUserName & "!"
End Sub
In the Login form, call the sub of the menu form like this :
DoCmd.OpenForm "Menu", acNormal
Form_Menu.Pass_Login_Info un
Use a Global variables to store the user info
It's the simpler and best approach to me because the Username is something that should be known and retrieved throughout all your application.
Just create a module, and in top of it add a public variable, that will be usable from anywhere
Public USER_NAME as string
In your login form :
USER_NAME = un
In the Load event of your Menu form
Private Sub Form_Load()
Me.Label4 = "Hi " & USER_NAME & "!"
End Sub
Option Compare Database
Private Sub Command1_Click()
Dim UserLevel As Integer
If IsNull(Me.txtLoginID) Then
MsgBox "Please enter LoginID", vbInformation, "LoginID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("UserLogin", "tbluser", "UserLogin='" & Me.txtLoginID.Value & "'"))) Or _
(IsNull(DLookup("UserLogin", "tbluser", "Password='" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect LoginID or Password"
Else
UserLevel = DLookup("UserSecurity", "tbluser", "UserLogin = '" & Me.txtLoginID.Value & "'")
DoCmd.Close
If UserLevel = 1 Then
MsgBox "LoginID and Password correct"
DoCmd.OpenForm "ali"
Else
DoCmd.OpenForm "customer"
End If
End If
End If
End Sub
This is code for login form.I have created. When I close my MS Access and open again my project login form cannot work.
On a form load event, I have it check the access level of the user. if it is below a certain threshold or they never signed in, then I have the line
Cancel = true
However, the form loads regardless. Here is the entire form load event.
Private Sub Form_Load()
If IsNull(Forms!frmUsernamePassword!txtUserName) Then
MsgBox "You need to enter a username or password first.", vbOKOnly + vbExclamation
Cancel = -1
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 1 Then
MsgBox "Your Security Level is 1", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 2 Then
MsgBox "Your Security Level is 2", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]= '" & Forms!frmUsernamePassword!txtUserName & "'") = 3 Then
MsgBox "Your Security Level is 3", vbOKOnly
ElseIf DLookup("[AccessLevel_ID]", "tblUsernamePasswords", "[txtUserName]='" Forms!frmUsernamePassword!txtUserName & "'") = 4 Then
MsgBox "Your Security Level is 4", vbOKOnly
End If
End Sub
sorry for the formatting issures.
Form load does not have a cancel argument. Use form open for cancel. Setting contents of controls must occur in form load.
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'")