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 (1 Viewer)

musmoniem

Member
Local time
Today, 07:53
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
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:53
Joined
Oct 29, 2018
Messages
21,528
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.
 

musmoniem

Member
Local time
Today, 07:53
Joined
Apr 30, 2020
Messages
30
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

  • adding permissions for every user.accdb
    1.2 MB · Views: 137

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
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:

musmoniem

Member
Local time
Today, 07:53
Joined
Apr 30, 2020
Messages
30
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
 

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
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 😉
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:53
Joined
Sep 21, 2011
Messages
14,417
You also have Exit Sub after End Sub ? :confused:
Good case for a Select Case statement as well I would have thought?
 

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
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
 

Cronk

Registered User.
Local time
Today, 15:53
Joined
Jul 4, 2013
Messages
2,774
In #1, there is Exit Sub after End Sub. This is omitted in #6
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:53
Joined
Sep 21, 2011
Messages
14,417
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.?
 

cheekybuddha

AWF VIP
Local time
Today, 06:53
Joined
Jul 21, 2014
Messages
2,318
I hope the first user in your Users table doesn't have SecurityLevel = 'Admin'

Try using:
Me.txtUsername: Hello
Me.txtPassword: ' OR '1'='1
 

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
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
 

musmoniem

Member
Local time
Today, 07:53
Joined
Apr 30, 2020
Messages
30
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 :'(
 

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
OK step through the code and see what it is recording as the entered password.
Is the password encrypted? It should be for security!
 

musmoniem

Member
Local time
Today, 07:53
Joined
Apr 30, 2020
Messages
30
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

  • adding permissions for every user.accdb
    1.2 MB · Views: 126
Last edited:

isladogs

MVP / VIP
Local time
Today, 06:53
Joined
Jan 14, 2017
Messages
18,258
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

Top Bottom