Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Disabling buttons based on logon

Posted on 2011-02-11
22
204 Views
Last Modified: 2012-05-11
Hi all,

Can you aid me with the best way to disable certain buttons based on user ID?  I have the standard module setup called "modfOSUserName"

I also have a table of users but am unsure on the best way to proceed as there is going to be a real mix on combinations.  I.e. Person A has access to every button, Person B has access to two buttons, Person C has access to buttons 2,4 & 6

Cheers

Chris
0
Comment
Question by:smods
  • 12
  • 10
22 Comments
 
LVL 11

Expert Comment

by:Runrigger
ID: 34869995
do you have groups of people which have access to specific buttons?
e.g.
User
Manager
Admin
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34870032
I traditionally use the TAG property of the field, which could be classed as Access Level;

e.g

ADMIN
MANAGER
USER

then do somthing like this;
Public Function CheckMandatoryFields(ctrls As Controls) As Boolean
Dim ctl As Control
Dim sTag As String

    For Each ctl In ctrls
        On Error GoTo IamNotaControl
        sTag = ctl.Tag
       
        If UserAccess = "Admin" Then
            ctl.Enabled = True
        ElseIf UserAccess = "Manager" Then
            If sTag = "MANAGER" Or sTag = "USER" Then
                ctl.Enabled = True
            Else
                ctl.Enabled = False
            End If
        Else
            If sTag = "USER" Then ctl.Enabled = True
        End If
IamNotaControl:
        Resume Continue
Continue:
    Next
End Function
0
 

Author Comment

by:smods
ID: 34870095
Hi Runrigger,

I don't have anything setup as of yet.  

I have the module to obtain the username and this code in the On Open event of the main form.

Private Sub Form_Open(Cancel As Integer)

    If DCount("User", _
              "tbl_authorisedusers", _
              "User = '" & fOSUserName & "'") = 0 Then
        MsgBox "You are not allowed to use the application.  Please contact x if you require access."
        Cancel = True
        DoCmd.Quit
    End If

End Sub

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 11

Expert Comment

by:Runrigger
ID: 34870116
Add a field to this table --->>>tbl_authorisedusers, call it USERTYPE

sUserAccess = DLookup("USERYPE", "tbl_authorisedusers", "User = '" & fOSUserName & "'")

make sUserAccess a global variable if you like and then call that sub and pass through to it the form controls (you will be able to use this for all of your forms then);

**************
Call DisableControls Me.Controls
**************

Public Sub DisableControls(ctrls As Controls)
Dim ctl As Control
Dim sTag As String

    For Each ctl In ctrls
        On Error GoTo IamNotaControl
        sTag = ctl.Tag
       
        If sUserAccess = "Admin" Then
            ctl.Enabled = True
        ElseIf sUserAccess = "Manager" Then
            If sTag = "MANAGER" Or sTag = "USER" Then
                ctl.Enabled = True
            Else
                ctl.Enabled = False
            End If
        Else
            If sTag = "USER" Then ctl.Enabled = True
        End If
IamNotaControl:
        Resume Continue
Continue:
    Next
End Sub


0
 

Author Comment

by:smods
ID: 34870126
Cheers Runrigger.

Can you clarify this part for me?  I'm poor with access!

sUserAccess = DLookup("USERYPE", "tbl_authorisedusers", "User = '" & fOSUserName & "'")

make sUserAccess a global variable if you like and then call that sub and pass through to it the form controls (you will be able to use this for all of your forms then);
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34870216
Add a normal module, call it whatever you want;

Add this code;

'=========================================
Public sUserType as String

'=========================================
Public Sub DisableControls(ctrls As Controls)
Dim ctl As Control
Dim sTag As String

    For Each ctl In ctrls
        On Error GoTo IamNotaControl
        sTag = ctl.Tag
       
        If sUserAccess = "Admin" Then
            ctl.Enabled = True
        ElseIf sUserAccess = "Manager" Then
            If sTag = "MANAGER" Or sTag = "USER" Then
                ctl.Enabled = True
            Else
                ctl.Enabled = False
            End If
        Else
            If sTag = "USER" Then ctl.Enabled = True
        End If
IamNotaControl:
        Resume Continue
Continue:
    Next
End Sub



Then add this line in your form opencode;

sUserAccess = DLookup("USERTYPE", "tbl_authorisedusers", "User = '" & fOSUserName & "'")

You will need to have added the USERTYPE field to the table though.

For each user, define their type as USER/MANAGER/ADMIN

Then for all of the controls on the form you wish to enable/disable, add to the TAG property of each control the value USER (available to all), MANAGER (available to ADMIN and MANAGER only) and ADMIN (available to ADMIN only)

Finally, also on the Form Open event, add the following code;

'==============================
Call DisableControls Me.Controls

You form open code will therefore look like this;

Private Sub Form_Open(Cancel As Integer)

    If DCount("User", _
              "tbl_authorisedusers", _
              "User = '" & fOSUserName & "'") = 0 Then
        MsgBox "You are not allowed to use the application.  Please contact x if you require access."
        Cancel = True
        DoCmd.Quit
    End If

sUserAccess = DLookup("USERTYPE", "tbl_authorisedusers", "User = '" & fOSUserName & "'")
Call DisableControls Me.Controls


End Sub
0
 

Author Comment

by:smods
ID: 34870620
Thanks.

When opening the form I get the error:  "Compile error: Variable not defined"

sUserAccess is highlighted in the above code and

Call DisableControls Me.Controls is also highlighted in red.

Regards

Chris
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34871696
Have you added a module?

You need to have "Public" declarations of the;

sUserType variable
DisableControls sub-routine

Please attache a database without the data, other than the User info and let me take a look
0
 

Author Comment

by:smods
ID: 34887070
Here you go!

Cheers

Chris
Copy-of-ee5.mdb
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34887139
Three things;

 - the opening event's first line was in there twice, so I removed one
 - the sUserAccess variable needed to be renamed (my fault) = sUserType
 - the syntax was wrong on calling the sub (my fault) = Call DisableControls(me.Controls)

1) try adding a user name and modify the user type
2) do not forget to amend the tag property of the buttons (also try them)
30 The 3 levels of access at the moment are Admin/Manager/User


Copy-of-ee5.mdb
0
 

Author Comment

by:smods
ID: 34887210
Thanks

The check works when opening the database but not the disabling of the buttons?

Regards

Chris
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34887291
See my comment here from my last post

----->>>>>>>>>>>
2) do not forget to amend the tag property of the buttons (also try them)
30 The 3 levels of access at the moment are Admin/Manager/User
----->>>>>>>>>>>

ee-Button-TAG.JPG
0
 

Author Comment

by:smods
ID: 34887307
Yep! I've got myself setup as a user and two buttons.  One set as manager and one set as admin and I can click them both!
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34887313
ok, will have a look, what is your username?
0
 

Author Comment

by:smods
ID: 34887334
CIRY
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34887462
OK, try this, the module was using a wrong user access varialble.

I have initially tagged you as a "User"

The three buttons on the main form have been tagged;

Admin
Manager
User

Try it with you as User, then manager, then admin (just change the vaule in the table)
Copy-of-ee5.mdb
0
 

Author Comment

by:smods
ID: 34887501
Working like a treat!

Is it easy to add another level to this?  I.e. The situation I am predicting is that there will be 2-3 groups of users all needing access to various parts.  So User1, User2, User3
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34887579
The approach I have given you is for an decreasing level of access (Admin-->>Manager-->>User)

If your controls are split in to groups, you can adopt a different method

e.g.
1) button1 available to usergroup1 only
2) button2 avaliable to usergroup2 only
3) button3 avaliable to usergroup3 only

then the routine can be written like attached (the way I have written it should be self explanatory)

NOTE - On your Exit button, change the TAG property to ALL;



'=========================================
Public Sub DisableControls(ctrls As Controls)
Dim ctl As Control
Dim sTag As String

    For Each ctl In ctrls
        On Error GoTo IamNotaControl
        sTag = ctl.Tag
        'The next line disables every button, other than buttons available to everyone
        If Not(sTag="ALL") Then ctl.Enabled = False
        

        'The following line enables every button for Admin users
        If sUserType = "Admin" Then ctl.Enabled = True
        'The following conditions enable buttons only when access is satisfied
        If sUserType = "usergroup1" and sTag="usergroup1" Then ctl.Enabled=True
        If sUserType = "usergroup2" and sTag="usergroup2" Then ctl.Enabled=True
        If sUserType = "usergroup3" and sTag="usergroup3" Then ctl.Enabled=True
        If sUserType = "usergroup4" and sTag="usergroup4" Then ctl.Enabled=True
'etc, etc

IamNotaControl:
        Resume Continue
Continue:
    Next
End Sub

Open in new window

0
 

Author Comment

by:smods
ID: 34888133
But I wouldn't be able to have a combination of both?

Or can you add more than one tag?
0
 
LVL 11

Accepted Solution

by:
Runrigger earned 500 total points
ID: 34888361
You can have more that one value in the tag, but you will need to separate the values with a delimiter, e.g.

TAG Structure like this = UserGroup1|UserGroup2|UserGroup3|UserGroup4
TAG Value like this (example) = Yes|No|Yes|Yes (meaning groups 1,3&4 have access to this button)

You can test as follows;

(Note: I have written the below on the fly, it should work, but try to work it out if it doesn't work initially)

'=========================================
Public Sub DisableControls(ctrls As Controls)
Dim ctl As Control
Dim sTag() As String 'This declares sTag as a string array

    For Each ctl In ctrls
        On Error GoTo IamNotaControl
        'The next line splits the TAG property in to the array using | (pipe) as the delimiter
        sTag = Split(ctl.Tag,"|")
        'The next line disables every button, other than buttons available to everyone
        If Not(sTag(0)="ALL") Then ctl.Enabled = False
        

        'The following line enables every button for Admin users
        If sUserType = "Admin" Then ctl.Enabled = True
        'The following conditions enable buttons only when access is satisfied
        'Remember that arrays are zero based
        If sUserType = "usergroup1" and sTag(0)="Yes" Then ctl.Enabled=True
        If sUserType = "usergroup2" and sTag(1)="Yes" Then ctl.Enabled=True
        If sUserType = "usergroup3" and sTag(2)="Yes" Then ctl.Enabled=True
        If sUserType = "usergroup4" and sTag(3)="Yes" Then ctl.Enabled=True
'etc, etc

IamNotaControl:
        Resume Continue
Continue:
    Next
End Sub

Open in new window

0
 

Author Comment

by:smods
ID: 34888532
Works a treat RunRigger

Many thanks.
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34888543
;-)

You are welcome Chris
Regards
Dave
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question