Solved Difficulty with login and permission (1 Viewer)

Mylton

Member
Local time
Today, 00:12
Joined
Aug 3, 2019
Messages
123
Good afternoon
I'm starting this study of login associated with permission by group to later implement the permission to execute or not the tasks by user action in the form.

There are two questions:
1) The login system for Admin and test 01 users opens and shows the name of the logged-in person in the main menu.
However Login Test 2 and Test 3 the command button doesn't even work.
I've seen, corrected and tried... and I don't find the blessed error.

2) When I stop commenting the lines referring to the navigation command buttons (Bt_Nav_1, Bt_Nav_2 and Bt_Nav_3) in the Frm_Login coding to work according to the logged in user, it displays the error that it was unable to locate the field "Bt_Nav_1 or 2 or 3" referred to in your expression.





Below the entire coding of the form (also attached)

Option Compare Database

Private Sub Bt_Login_Click()
'Crio a variavel Nivel usuário
Dim NivelUsuario As Integer

'Crio a variavel TempSenha que será a senha temporária dada ao novo usuário
Dim TempSenha As String

'Crio a variavel ID
Dim ID As Integer

'Crio a variavel paara Usuário atual do form menu principal
Dim UsuarioTemp As String

If IsNull(Me.txt_Usuario) Then
MsgBox "Entre com nome do usuário.", vbInformation, "Nome do Usuário Necessário"
Me.txt_Usuario.SetFocus

ElseIf IsNull(Me.txt_Senha) Then
MsgBox "Digite a senha.", vbInformation, "Senha Necessária"
Me.txt_Senha.SetFocus
Else
' Nome do usuario/Tbl Usuario, Tbl Usuario, Nome do usuario/Tabl Usuario /Nome campo usuario Form
If (IsNull(DLookup("Nome_Usuario", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'"))) Or _
IsNull(DLookup("Senha_Usuario", "Tbl_Usuario", "Senha_Usuario='" & Me.txt_Senha.Value & "'")) Then
' Acima = Senha do usuario/Tbl Usuario, Tbl Usuario, Senha do usuario/Tabl Usuario /Nome campo senha do usuario Form
MsgBox "Nome do usuário ou senha errados.", vbInformation, "Acesso negado"
Me.txt_Usuario = ""
Me.txt_Senha = ""
Me.txt_Usuario.SetFocus

Else
'Uso a variavel NivelUsuario
NivelUsuario = DLookup("[Nivel_Usuario]", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel TempSenha
TempSenha = DLookup("[Senha_Usuario]", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel ID
ID = DLookup("Id_Usuario", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel UsuárioTemp
UsuarioTemp = Me.txt_Usuario


If (TempSenha = "Senha_Usuario") Then
DoCmd.OpenForm "Frm_Cad_Usuario", , , "[Id_Usuario]=" & ID

Else

'Função usuario logado Administrador
If NivelUsuario = 1 Then
MsgBox "Acesso permitido", vbInformation, "Autenticação efetuada com sucesso"
DoCmd.OpenForm "Frm_MenuInicial"
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
Forms![Frm_MenuInicial]![Bt_Nav_1].enable = True
Forms![Frm_MenuInicial]![Bt_Nav_2].enable = True
Forms![Frm_MenuInicial]![Bt_Nav_3].enable = True

'Função usuario logado usuário
ElseIf NivelUsuario = 2 Then
DoCmd.OpenForm "Frm_MenuInicial"
'Coloca nome usuario logado no form
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
'Habiliata botões de navegação conforme a função do usuario logado
' Forms![Frm_MenuInicial]![Bt_Nav_1].enable = True
' Forms![Frm_MenuInicial]![Bt_Nav_2].enable = True
' Forms![Frm_MenuInicial]![Bt_Nav_3].enable = True

'Função usuario logado Teste
If NivelUsuario = 3 Then
MsgBox "Acesso permitido", vbInformation, "Autenticação efetuada com sucesso"
DoCmd.OpenForm "Frm_MenuInicial"
'Coloca nome usuario logado no form
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
'Habiliata botões de navegação conforme a função do usuario logado
' Forms![Frm_MenuInicial]![Bt_Nav_1].enable = False
' Forms![Frm_MenuInicial]![Bt_Nav_2].enable = False
' Forms![Frm_MenuInicial]![Bt_Nav_3].enable = False

End If
End If
End If
End If
End If

End Sub

'Private Sub Form_Load()
' DoCmd.ShowToolbar "Ribbon", acToolbarNo
'End Sub


Private Sub bt_close_Click()
DoCmd.Quit
End Sub

Private Sub bt_CriarConta_Click()
DoCmd.Close acForm, "Frm_Login"
DoCmd.OpenForm "Frm_Novo_Usuario"
End Sub

Private Sub sl_ExibirSenha_AfterUpdate()
' exibir senha
If sl_ExibirSenha = True Then
txt_Senha.InputMask = ""
Else
txt_Senha.InputMask = "Senha_Usuario"
End If

I apologize, but I didn't have enough time to translate the forms from Portuguese - Br into English.


Thanks.
 

Attachments

  • TesteLogin 01.zip
    92 KB · Views: 107

Gasman

Enthusiastic Amateur
Local time
Today, 08:12
Joined
Sep 21, 2011
Messages
14,299
Please post code within code tags. :(
Even in English that would still be hard to read. :(
It keeps the indentation, which I hope you are using?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:12
Joined
Feb 19, 2002
Messages
43,275
I know you didn't ask for an alternate method but here's a link if you are interested.
 

Mylton

Member
Local time
Today, 00:12
Joined
Aug 3, 2019
Messages
123
I don't know if I understood correctly.

I did it with spacing in the coding.
But here I am not able to do it.
I put all the code in this PDF and I will try to print the screen
 

Attachments

  • Option Compare Database.pdf
    59.5 KB · Views: 80
  • Print 01.png
    Print 01.png
    66.5 KB · Views: 69
  • Print 02.png
    Print 02.png
    71.9 KB · Views: 72

Gasman

Enthusiastic Amateur
Local time
Today, 08:12
Joined
Sep 21, 2011
Messages
14,299
Use code tags, the </> icon.
Code:
This is after using the </> icon
 

Mylton

Member
Local time
Today, 00:12
Joined
Aug 3, 2019
Messages
123
Code:
Option Compare Database

Private Sub Bt_Login_Click()
'Crio a variavel Nivel usuário
Dim NivelUsuario As Integer

'Crio a variavel TempSenha que será a senha temporária dada ao novo usuário
Dim TempSenha As String

'Crio a variavel ID
Dim ID As Integer

'Crio a variavel paara Usuário atual do form menu principal
Dim UsuarioTemp As String

If IsNull(Me.txt_Usuario) Then
MsgBox "Entre com nome do usuário.", vbInformation, "Nome do Usuário Necessário"
Me.txt_Usuario.SetFocus

ElseIf IsNull(Me.txt_Senha) Then
MsgBox "Digite a senha.", vbInformation, "Senha Necessária"
Me.txt_Senha.SetFocus
Else
' Nome do usuario/Tbl Usuario, Tbl Usuario, Nome do usuario/Tabl Usuario /Nome campo usuario Form
If (IsNull(DLookup("Nome_Usuario", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'"))) Or _
IsNull(DLookup("Senha_Usuario", "Tbl_Usuario", "Senha_Usuario='" & Me.txt_Senha.Value & "'")) Then
' Acima = Senha do usuario/Tbl Usuario, Tbl Usuario, Senha do usuario/Tabl Usuario /Nome campo senha do usuario Form
MsgBox "Nome do usuário ou senha errados.", vbInformation, "Acesso negado"
Me.txt_Usuario = ""
Me.txt_Senha = ""
Me.txt_Usuario.SetFocus

Else
'Uso a variavel NivelUsuario
NivelUsuario = DLookup("[Nivel_Usuario]", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel TempSenha
TempSenha = DLookup("[Senha_Usuario]", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel ID
ID = DLookup("Id_Usuario", "Tbl_Usuario", "Nome_Usuario='" & Me.txt_Usuario.Value & "'")
'Uso a variavel UsuárioTemp
UsuarioTemp = Me.txt_Usuario


If (TempSenha = "Senha_Usuario") Then
DoCmd.OpenForm "Frm_Cad_Usuario", , , "[Id_Usuario]=" & ID

Else

'Função usuario logado Administrador
If NivelUsuario = 1 Then
MsgBox "Acesso permitido", vbInformation, "Autenticação efetuada com sucesso"
DoCmd.OpenForm "Frm_MenuInicial"
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
Forms![Frm_MenuInicial]![Bt_Nav_1].enable = True
Forms![Frm_MenuInicial]![Bt_Nav_2].enable = True
Forms![Frm_MenuInicial]![Bt_Nav_3].enable = True

'Função usuario logado usuário
ElseIf NivelUsuario = 2 Then
DoCmd.OpenForm "Frm_MenuInicial"
'Coloca nome usuario logado no form
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
'Habiliata botões de navegação conforme a função do usuario logado
' Forms![Frm_MenuInicial]![Bt_Nav_1].enable = True
' Forms![Frm_MenuInicial]![Bt_Nav_2].enable = True
' Forms![Frm_MenuInicial]![Bt_Nav_3].enable = True

'Função usuario logado Teste
If NivelUsuario = 3 Then
MsgBox "Acesso permitido", vbInformation, "Autenticação efetuada com sucesso"
DoCmd.OpenForm "Frm_MenuInicial"
'Coloca nome usuario logado no form
Forms![Frm_MenuInicial]![Txt_Usuario_Atual] = UCase(UsuarioTemp)
'Habiliata botões de navegação conforme a função do usuario logado
' Forms![Frm_MenuInicial]![Bt_Nav_1].enable = False
' Forms![Frm_MenuInicial]![Bt_Nav_2].enable = False
' Forms![Frm_MenuInicial]![Bt_Nav_3].enable = False

End If
End If
End If
End If
End If

End Sub

'Private Sub Form_Load()
' DoCmd.ShowToolbar "Ribbon", acToolbarNo
'End Sub


Private Sub bt_close_Click()
DoCmd.Quit
End Sub

Private Sub bt_CriarConta_Click()
DoCmd.Close acForm, "Frm_Login"
DoCmd.OpenForm "Frm_Novo_Usuario"
End Sub

Private Sub sl_ExibirSenha_AfterUpdate()
' exibir senha
If sl_ExibirSenha = True Then
txt_Senha.InputMask = ""
Else
txt_Senha.InputMask = "Senha_Usuario"
End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:12
Joined
Sep 21, 2011
Messages
14,299
It is not. :(
Copy the code from your db to here, but within code tags.
That looks like you just copied the code from your original post. The indentation has already been removed? :(

Would you want to try and understand code that looks like that?, I certainly do not. :)
 

Mylton

Member
Local time
Today, 00:12
Joined
Aug 3, 2019
Messages
123
I will translate for you and attach until tomorrow

thanks
 

MarkK

bit cruncher
Local time
Today, 00:12
Joined
Mar 17, 2004
Messages
8,181
Your If block for NivelUsuario = 3 is INSIDE your ElseIf block for NivelUsuario = 2, and therefore never executes. This error is directly a result of messy code and inconsistent indentation. Here's your code tidied up, and look how easy the error is to find...

ss.png


Bugs hide easily in messy code. It took me about 10 minutes to correct your indentation. If you corrected your own indentation, you would find this bug in 10 minutes.

Indentation Rule: Every part of an If...ElseIf...Else...End If block should be indented to the same depth. This is not for the compiler, this is for you, the programmer, to properly understand your own code.

Cheers,
 

Mylton

Member
Local time
Today, 00:12
Joined
Aug 3, 2019
Messages
123
good morning
good to hear one more.
I don't imagine that the way I "ruined" it visually interfered.
thanks for the information.
I will try to find literature that explains what you said to get it right.

thanks.
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:12
Joined
Jan 23, 2006
Messages
15,379
Mylton,

You should use
Option Explicit as the second line in every module. At some point it will help you with spelling mistakes and unDimmed variables.

Markk's Indentation Rule is key to help you (and others) to understand the code.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:12
Joined
Feb 19, 2002
Messages
43,275
I don't imagine that the way I "ruined" it visually interfered.
Correctly indenting the code gives YOU and other humans visual clues. Without them, code is just a mass of text and you need to read AND UNDESRTAND every single line in order to find problems. You have to keep in mind where you are in the hierarchy of a complex nested If. Most people, even experienced developers have trouble seeing the forest for the trees. Bridge players always sort their cards by suit (except for my expert partner who was a genius) and expert programmers always indent their code correctly;)
 

KitaYama

Well-known member
Local time
Today, 16:12
Joined
Jan 6, 2022
Messages
1,541
Your If block for NivelUsuario = 3 is INSIDE your ElseIf block for NivelUsuario = 2, and therefore never executes. This error is directly a result of messy code and inconsistent indentation. Here's your code tidied up, and look how easy the error is to find...

View attachment 108910

Bugs hide easily in messy code. It took me about 10 minutes to correct your indentation. If you corrected your own indentation, you would find this bug in 10 minutes.

Indentation Rule: Every part of an If...ElseIf...Else...End If block should be indented to the same depth. This is not for the compiler, this is for you, the programmer, to properly understand your own code.

Cheers,
@MarkK How have you set your colors to have that result? Is Normal Text set to red and Identifier Text to Auto Foreground?

thanks.
 

Users who are viewing this thread

Top Bottom