Disabling all controls on a form

Shimotsuma

New member
Local time
Today, 09:24
Joined
May 2, 2013
Messages
5
Hi all,

I want to disable all controls on a form based on a user role.
I have have the following code:
Code:
Private Sub Form_Load()

Dim ctl As Control

For Each ctl In Me.Controls

    If strRole <> "Administrative Assistant" Then
        
                ctl.Enabled = False
            
            Else
                ctl.Enabled = True
                
                         
     
    End If

Next

End Sub
Of course the ctl.enabled property does not exist. I can't seem to find a way around this without typing all the names of the controls...
The controls have to remain visible.

Anyone can point me in the right direction?

Thanks in advance!
 
I guess you could check the control type - obviously only certain control types have an enabled property. Take a look here.

Also, you may want to consider putting your For Each loop inside your IF strRole contruct. The point being you only have to test strRole once and not for every control.

hth
Chris
 
Hi,
I went down the module route, here's a little sample. I'm guessing you're holding the user role somewhere so feel free to adjust the following to fit your needs. :cool:
On the load of each form add 'SetSecurity' then to hide or disable controls as you wish.
Code:
Public Function SetSecurity()
On Error GoTo Err_Handler
Dim intSec As Integer
Dim frmCurrentForm As Form
        intSec = [Forms]![switchboard].LLSecurity
        Select Case intSec
        
            Case 999 'Super User settings
                DoCmd.ShowToolbar "LL", acToolbarYes
                Set frmCurrentForm = Screen.ActiveForm
                With frmCurrentForm
                    .ShortcutMenu = True
                End With
            
            Case 899 'Admin - No access to User List controls
                DoCmd.ShowToolbar "LL", acToolbarYes
                Set frmCurrentForm = Screen.ActiveForm
                With frmCurrentForm
                    .ShortcutMenu = True
                    .selBR = DLookup("[Branch]", "LLUsers", "[UserName] ='" & [Forms]![switchboard].[UserName] & "'")
                    .selBR.Locked = True
                    .selBR.Enabled = False
                End With
 
etc etc
 
end select
Exit Function
cheers
Matt
 
If you have a sub form then that is also a Control.

Just thought I might throw that in.

Do you only have two types of users.

Another way to do this is with the code behind the control. If the user is a certain type then do .............................. if not do nothing.
 
Matt's way is a good way to take care of this.

Also check out the declaration at the start of the sub. I have used the following in one of my sub to disable some controls.

Dim control As control
Dim Ctrls As Controls
Set Ctrls = Me.Controls

________________
Ashok
 
You might be able to handle the whole thing by setting a couple of form properties which will be a lot quicker and won't result in form flicker or sluggishness.

Code:
If UserType = 1 Then
    Me.AllowEdits = False
    Me.AllowDeletions = False
    Me.AllowAdditions = False
Else
    Me.AllowEdits = True
    Me.AllowDeletions = True
    Me.AllowAdditions = True
End If
 
Another possibility:-

Code:
Private Sub Form_Load()

    If strRole = "Administrative Assistant" Then
        Me.RecordsetType = 0    [color=green]' Dynaset[/color]
    Else
        Me.RecordsetType = 2    [color=green]' Snapshot[/color]
    End If
    
End Sub

Chris.
 
I have a Function that I call whenever I need to lock down all of the controls and I use it in the load event of my form. With this code, you can still navigate through your subform, but the user cannot enter any data until the the controls are unlocked.

Code:
Private Function Lockdown() 'locks controls at load
 
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = True
            tb.BackColor = vbWhite
        End If
    Next
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = True
            cb.BackColor = vbWhite
        End If
    Next
    For Each subF In Me.Controls
        If TypeOf subF Is SubForm Then
            subF.Locked = True
        End If
    Next
 
End Function

tb = textboxes
cb = combo boxes
subF = subform

the .backcolor will change the color of the textbox and combo box; I use this to let the user know that the controls can be edited when the color changes.

For example: I use white for locked and when the user clicks on my edit button, then the controls turn a light greenish blue color.


Then when I want to unlock the forms I just use the reverse of the above code in a Function called "UnlockFields"

Code:
Private Function UnlockFields() 'unlocks controls
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = False
            tb.BackColor = &HFAFAD2
        End If
    Next
 
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = False
            cb.BackColor = &HFAFAD2
        End If
    Next
    For Each subF In Me.Controls
        If TypeOf subF Is SubForm Then
            subF.Locked = False
 
        End If
    Next
End Function


Oh, and don't forget to declare the controls:

Code:
Dim tb As Control
Dim cb As Control
Dim subF As Control

So, I guess you could do:

Code:
If strRole <> "Administrative Assistant" Then
        
               Lockdown
            
            Else
                UnlockFields
                
    End If
 

Users who are viewing this thread

Back
Top Bottom