Enabling and disabling controls via a "access level"

dcf1999

Registered User.
Local time
Yesterday, 18:16
Joined
Dec 19, 2018
Messages
26
Hello All. So I'm creating a database for the fire department I belong to. It's basically to log calls and create reports based on those calls. I will also log other things like problem tickets, apparatus checks, etc... Anyways I have it set up with access permissions. Each employee has a login and when that person logs in, it looks at the "employee" database and sees what "level" they are assigned. Currently I have 3 levels.

Level 1 - General user
Level 2 - Quality Assurance Personal
Level 3 - Admin (chief)

On each form, the buttons for features are either enabled or disabled depending on that security level. I'm doing this buy assigning a the button's tag value either 1, 2, or 3. After the user logs in on the main form, it runs a function to enable or disable certain buttons based on their tag value. Any form that opens also runs this function.

It works to enable the buttons that need to be enabled but when it comes to disabling (user logs out) it doesn't work and says the control doesn't support the property. After doing some debugging, the function looks at all controls, including labels (which don't have an "enabled" property) so that's why it's erroring out.

When the function runs, it looks to see if the .tag is not null and is >= user_perm (a global variable). The controls that I don't need enabled / disabled don't have anything in their tag values (which, i assume is considered null?) so it shouldn't even be looking at the labels because there is no value in their tag property.

Below is the function code. On the form's controls, I only have a value in the tag property if I need it enabled / disabled for access restrictions.

Code:
Public Sub ValidateFormControls(myform As Form)
    Dim ctl As Control
    With myform
        For Each ctl In .Controls
            With ctl
                If IsNull(.Tag) = False And .Tag <= user_perm Then
                    .Enabled = True
                Else
                    .Enabled = False
                End If
            End With
        Next ctl
    End With
End Sub

Thanks
 
Try this (not tested but should be OK)

Code:
Public Sub ValidateFormControls(myform As Form)
    Dim ctl As Control
    With myform
        For Each ctl In .Controls
            [B][COLOR="DarkRed"]Select Case ctl.ControlType[/COLOR][/B]
        
           [B][COLOR="DarkRed"] Case acLabel, acImage, acLine, acRectangle, acPageBreak
                 [COLOR="Green"] 'no code here - these can't be disabled[/COLOR]
            Case Else[/COLOR][/B]
              With ctl
                If IsNull(.Tag) = False And .Tag <= user_perm Then
                    .Enabled = True
                Else
                    .Enabled = False
                End If
              End With 
           [B][COLOR="darkred"] End Select[/COLOR][/B]
        Next ctl
    End With
End Sub

I would recommend you use a Public Sub (or Function) in a standard module rather than repeat code in each form

You might want to look at my SetControls example app - http://www.mendipdatasystems.co.uk/set-controls/4594398114
 
Thank you everyone! It works!!!! golly something so simple.

I would recommend you use a Public Sub (or Function) in a standard module rather than repeat code in each form

I do have that function in a module and not in each form. I have each form call that function from the module
 
Excellent
The reason I suggested looking at my app is it shows you which controls can be disabled, locked or hidden.
I got that list of controls to exclude based on my own code in that app
 
Last edited:

Users who are viewing this thread

Back
Top Bottom