Hide Form Controls based on User

  • Thread starter Keiron via AccessMonster.com
  • Start date
K

Keiron via AccessMonster.com

Hi I have a main page that loads after using the login page to enter username
and password, when the main page loads i want the controls on the main page
to be hidding if the user is not a manager or admin. I have the following
code and it works if i remove the ElseIF part of the code that looks up if
they are a manager.

any help would be greatly appreciated.

Cheers
Keiron

Private Sub Form_Load()
On Error GoTo Err_Form_Load

'Put the username in the text box
WorkerName = Forms![login]![txtuser]
'Close the login form
DoCmd.Close acForm, "Login"

'Get their username
'WorkerName = Forms![frm_main]![WorkerName]
'See if they are admin or have admin rights
If WorkerName = "admin" Or DLookup("admin", "tbl_worker", "WorkerName = '" &
WorkerName & "'") = "Yes" Then
admin.Visible = True
Managers.Visible = True
ManagersReports.Visible = True
ElseIf DLookup("Manager", "tbl_worker", "WorkerName = '" & WorkerName & "'")
= "Yes" Then
Managers.Visible = True
ManagersReports.Visible = True
Else
admin.Visible = False
Managers.Visible = False
ManagersReports.Visible = False
End If

DoCmd.Maximize

Exit_Form_Load:
Exit Sub
Err_Form_Load:
MsgBox Err.Description
DoCmd.OpenForm "Login"
DoCmd.Close acForm, "frm_main"
Resume Exit_Form_Load

End Sub
 
T

tina

try changing the following section of your code slightly, as

ElseIf DLookup("Manager", "tbl_worker", "WorkerName = '" & WorkerName & "'")
= "Yes" Then
admin.Visible = False
Managers.Visible = True
ManagersReports.Visible = True

if that doesn't solve the problem, then be more specific. you said the code
works when you remove the ElseIf section, but you didn't describe what
happens when the ElseIf section runs.

hth
 
K

Keiron via AccessMonster.com

Hi i just tried that and it didnt work. when I click enter on the login page
it comes up with a error msg
"You cancelled the previous operation"

But if i remove the Elseif part of the code it loads up the main page. also
if i login as a user that has both admin and manager permissions it works. so
its reading the first part of the IF code but not the rest.

So im not sure what im doing wrong.

Cheers
Keiron
try changing the following section of your code slightly, as

ElseIf DLookup("Manager", "tbl_worker", "WorkerName = '" & WorkerName & "'")
= "Yes" Then
admin.Visible = False
Managers.Visible = True
ManagersReports.Visible = True

if that doesn't solve the problem, then be more specific. you said the code
works when you remove the ElseIf section, but you didn't describe what
happens when the ElseIf section runs.

hth
Hi I have a main page that loads after using the login page to enter username
and password, when the main page loads i want the controls on the main page
[quoted text clipped - 44 lines]
 
T

tina

okay, i'll assume all the following:

"admin" is a field in tbl_worker, with a data type of Text.
"Manager" is a field in tbl_worker, with a data type of Text.
the code is running from the Form_Load event of form "frm_main".
on the form, "WorkerName" is the name of a textbox control.
on the form, "admin" is the name of an object (perhaps a command button?)
on the form, "Managers" is the name of an object (again, command button?)
on the form, "ManagersReports" is the name of an object (command button?)

it's hard to tell for sure by looking at your code, since you don't
reference the current form, and you also don't explicitly declare variables
at the beginning of the procedure (you should).

note that if any of the above assumptions are incorrect, the code below will
probably not work as intended.

Private Sub Form_Load()

On Error GoTo Err_Form_Load

Dim blnAdmin As Boolean, blnMgr As Boolean
Dim strName As String

strName = Forms![login]![txtuser]
DoCmd.Close acForm, "Login"

Me!WorkerName = strName
blnAdmin = (strName = "admin" Or DLookup("admin", _
"tbl_worker", "WorkerName = '" _
& strName & "'") = "Yes")
blnMgr = (DLookup("Manager", "tbl_worker", _
"WorkerName = '" & strName & "'") = "Yes")

Me!admin.Visible = blnAdmin
Me!Managers.Visible = (blnAdmin Or blnMgr)
Me!ManagersReports.Visible = (blnAdmin Or blnMgr)

DoCmd.Maximize

Exit_Form_Load:
Exit Sub
Err_Form_Load:
MsgBox Err.Description
DoCmd.OpenForm "Login"
DoCmd.Close acForm, "frm_main"
Resume Exit_Form_Load

End Sub

hth


Keiron via AccessMonster.com said:
Hi i just tried that and it didnt work. when I click enter on the login page
it comes up with a error msg
"You cancelled the previous operation"

But if i remove the Elseif part of the code it loads up the main page. also
if i login as a user that has both admin and manager permissions it works. so
its reading the first part of the IF code but not the rest.

So im not sure what im doing wrong.

Cheers
Keiron
try changing the following section of your code slightly, as

ElseIf DLookup("Manager", "tbl_worker", "WorkerName = '" & WorkerName & "'")
= "Yes" Then
admin.Visible = False
Managers.Visible = True
ManagersReports.Visible = True

if that doesn't solve the problem, then be more specific. you said the code
works when you remove the ElseIf section, but you didn't describe what
happens when the ElseIf section runs.

hth
Hi I have a main page that loads after using the login page to enter username
and password, when the main page loads i want the controls on the main
page
[quoted text clipped - 44 lines]
 
K

Keiron via AccessMonster.com

Thanks Tina,

That worked i appreciate the time taken to help me. Im new to access and
have never used sql or VB until now, so it takes me a bit longer to do things
and they may not always be the best way to do things either.

Cheers
Keiron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top