I am planning to set user privilege based on the user’s group after they logon. I need helps to correct my code or approach.
I created a table named TUSERID that consist of three fields: UserID, UserName, Password, and Department.
The database open with logon form. After user select their username, enter password and click the cmdLogin button, the event code check the password and user department then open the Main_0 form.
Depending on the user’s department, I want to show/hide selected buttons in the Main_0 form.
I use the DoCmd.OpenForm line with cmdVendorRpt = True/False to control user’s privilege. cmdVendorRpt is one of the buttons that I want to control user's ability to see.
However, this does not work. The Main_0 form opened with all buttons shown no matter what the user department is.
Please help. Below is my code.
Thanks
I created a table named TUSERID that consist of three fields: UserID, UserName, Password, and Department.
The database open with logon form. After user select their username, enter password and click the cmdLogin button, the event code check the password and user department then open the Main_0 form.
Depending on the user’s department, I want to show/hide selected buttons in the Main_0 form.
I use the DoCmd.OpenForm line with cmdVendorRpt = True/False to control user’s privilege. cmdVendorRpt is one of the buttons that I want to control user's ability to see.
However, this does not work. The Main_0 form opened with all buttons shown no matter what the user department is.
Please help. Below is my code.
Thanks
Code:
Private Sub cmdLogin_Click()
Dim MyPassword As String
Dim MyGroup As String
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this
'matches value chosen in combo box
MyPassword = DLookup("[Password]", "TUSERID", "[UserID]=[cboEmployee]")
MyGroup = DLookup("[Department]", "TUSERID", "[UserID] = [cboEmployee]")
If txtPassword = MyPassword Then
DoCmd.Close acForm, "F_Logon", acSaveNo
If MyGroup = "Admin" Then
DoCmd.OpenForm "F_Main_0", , , , , , cmdVendorRpt = True
Else
DoCmd.OpenForm "F_Main_0", , , , , , cmdVendorRpt = False
End If
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
Comment