Solved

Disabling buttons based on logon

Posted on 2011-02-11
22
201 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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now