i am trying making a security levels for each user in the database i wrote the code and i had a bug with it any help

musmoniem

Member
Local time
Today, 03:21
Joined
Apr 30, 2020
Messages
30
i am trying making a security levels for each user in the database i wrote the code and i had a bug with it any help

bug.PNG
 
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.
 
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.

i accually did it but in didn't work :(
here is a sample
 

Attachments

Try 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
 
Last edited:
d
Try 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
didn't work :(

bug 2.PNG
 
Two things
1. Saying 'didn't work is no use at all. Give specific details
2. Posting screenshots of your code is unhelpful. Please post the code itself to make it easier for others to edit.

Having said that, I can see two full stops (periods) that crept into the code by mistake at the start of the line: If sname <>"' Then and the following line.
I also omitted an End If line at the end. Oops!
Try again...

Rich (BB 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 If

End Sub

Hopefully correct this time 😉
 
You also have Exit Sub after End Sub ? :confused:
Good case for a Select Case statement as well I would have thought?
 
You also have Exit Sub after End Sub ? :confused:
Good case for a Select Case statement as well I would have thought?

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
 
In #1, there is Exit Sub after End Sub. This is omitted in #6
 
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
No Colin, the o/p, just another option which might make it clearer for them.?
 
I hope the first user in your Users table doesn't have SecurityLevel = 'Admin'

Try using:
Me.txtUsername: Hello
Me.txtPassword: ' 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

If that needs explaining, try Googling SQL injecton
 
this
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
code worked ..but it can't recognize password .. every password it gives me wrong password :'(
 
OK step through the code and see what it is recording as the entered password.
Is the password encrypted? It should be for security!
 
OK step through the code and see what it is recording as the entered password.
Is the password encrypted? It should be for security!
no its not encrypted
i had been tired with it what could i do now .. any help please

thats the code :
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 If

End Sub
 

Attachments

Last edited:
Have a look at my Password Login example database which includes the use of encryption.
See if you can use some / all of that code
 

Users who are viewing this thread

Back
Top Bottom