Hi. Count how many Ifs you have and make sure you have the same number of End Ifs. Also, you have an Exit Sub outside of a Sub. That would give you another error.
Private Sub OK_Click()
If IsNull(Me.txtUsername) Then
MsgBox "Please Enter Username", vbInformation, " Username Required"
Me.txtUsername.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, " Password Required"
Me.txtPassword.SetFocus
Exit Sub
End If
' all have values then validate it agains the table
Dim sName As String
Dim UserLevel As String
sName = DLookup("Name", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'") & ""
UserLevel = Nz(DLookup("SecurityLevel", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'"))
If sName <> "" Then
If UserLevel = "Admin" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Admin"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
ElseIf UserLevel = "User" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As User"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
ElseIf UserLevel = "Viewer" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Viewer"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
Form_DesktoppForm.SignedUpForms.Visible = False
Form_DesktoppForm.AllowEdits = False
Else
MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
End If
End Sub
didn't workTry this
Code:Private Sub OK_Click() If IsNull(Me.txtUsername) Then MsgBox "Please Enter Username", vbInformation, " Username Required" Me.txtUsername.SetFocus Exit Sub End If If IsNull(Me.txtPassword) Then MsgBox "Please Enter Password", vbInformation, " Password Required" Me.txtPassword.SetFocus Exit Sub End If ' all have values then validate it agains the table Dim sName As String Dim UserLevel As String sName = DLookup("Name", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'") & "" UserLevel = Nz(DLookup("SecurityLevel", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'")) . If sName <> "" Then . If UserLevel = "Admin" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Admin" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") ElseIf UserLevel = "User" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As User" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") Form_DesktoppForm.AddUser.Visible = False ElseIf UserLevel = "Viewer" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Viewer" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") Form_DesktoppForm.AddUser.Visible = False Form_DesktoppForm.SignedUpForms.Visible = False Form_DesktoppForm.AllowEdits = False Else MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied" End If End Sub
Strongly recommend you add the line Option Explicit as the second line of your code module
Private Sub OK_Click()
If IsNull(Me.txtUsername) Then
MsgBox "Please Enter Username", vbInformation, " Username Required"
Me.txtUsername.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, " Password Required"
Me.txtPassword.SetFocus
Exit Sub
End If
' all have values then validate it agains the table
Dim sName As String
Dim UserLevel As String
sName = DLookup("Name", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'") & ""
UserLevel = Nz(DLookup("SecurityLevel", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'"))
If sName <> "" Then
If UserLevel = "Admin" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Admin"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
ElseIf UserLevel = "User" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As User"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
ElseIf UserLevel = "Viewer" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Viewer"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
Form_DesktoppForm.SignedUpForms.Visible = False
Form_DesktoppForm.AllowEdits = False
Else
MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
End If
End If
End Sub
You also have Exit Sub after End Sub ?
Good case for a Select Case statement as well I would have thought?
No Colin, the o/p, just another option which might make it clearer for them.?Hi Paul
Is that directed at me? If so I can't see any Exit Subs after End Sub
The code could definitely be improved but I was just trying to fix what was supplied
Hello
' OR '1'='1
I hope the first user in your Users table doesn't have SecurityLevel = 'Admin'
Try using:
Me.txtUsername:Hello
Me.txtPassword:' OR '1'='1
code worked ..but it can't recognize password .. every password it gives me wrong password :'(Private Sub OK_Click() If IsNull(Me.txtUsername) Then MsgBox "Please Enter Username", vbInformation, " Username Required" Me.txtUsername.SetFocus Exit Sub End If If IsNull(Me.txtPassword) Then MsgBox "Please Enter Password", vbInformation, " Password Required" Me.txtPassword.SetFocus Exit Sub End If ' all have values then validate it agains the table Dim sName As String Dim UserLevel As String sName = DLookup("Name", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'") & "" UserLevel = Nz(DLookup("SecurityLevel", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'")) If sName <> "" Then If UserLevel = "Admin" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Admin" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") ElseIf UserLevel = "User" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As User" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") Form_DesktoppForm.AddUser.Visible = False ElseIf UserLevel = "Viewer" Then MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Viewer" TempVars("tmpVarUserName") = sName DoCmd.Close acForm, Me.Name DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName") Form_DesktoppForm.AddUser.Visible = False Form_DesktoppForm.SignedUpForms.Visible = False Form_DesktoppForm.AllowEdits = False Else MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied" End If End If End Sub
no its not encryptedOK step through the code and see what it is recording as the entered password.
Is the password encrypted? It should be for security!
Private Sub OK_Click()
If IsNull(Me.txtUsername) Then
MsgBox "Please Enter Username", vbInformation, " Username Required"
Me.txtUsername.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, " Password Required"
Me.txtPassword.SetFocus
Exit Sub
End If
' all have values then validate it agains the table
Dim sName As String
Dim UserLevel As String
sName = DLookup("Name", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'") & ""
UserLevel = Nz(DLookup("SecurityLevel", "Users", "Username = '" & Me.txtUsername & "' And Password = '" & Me.txtPassword & "'"))
If sName <> "" Then
If UserLevel = "Admin" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Admin"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
ElseIf UserLevel = "User" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As User"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
ElseIf UserLevel = "Viewer" Then
MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed As Viewer"
TempVars("tmpVarUserName") = sName
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm FormName:="DesktoppForm", OpenArgs:=TempVars("tmpVarUserName")
Form_DesktoppForm.AddUser.Visible = False
Form_DesktoppForm.SignedUpForms.Visible = False
Form_DesktoppForm.AllowEdits = False
Else
MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
End If
End If
End Sub