Solved Difficulty with login and permission

Mylton

Member
Local time
Yesterday, 20:58
Joined
Aug 3, 2019
Messages
124
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

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?
 
I know you didn't ask for an alternate method but here's a link if you are interested.
 
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
    Option Compare Database.pdf
    59.5 KB · Views: 134
  • Print 01.png
    Print 01.png
    66.5 KB · Views: 112
  • Print 02.png
    Print 02.png
    71.9 KB · Views: 114
Use code tags, the </> icon.
Code:
This is after using the </> icon
 
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
 
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. :)
 
I will translate for you and attach until tomorrow

thanks
 
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,
 
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.
 
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.
 
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;)
 
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

Back
Top Bottom