Search

Want role-based security? Here's a simple way to control the controls

When you start working with user logins and role-based security in Access, there are all kinds of ways to control who sees what and what functions are allowed for each user level. I use Ribbons for top-level controls (menu picks). But if you need more granularity, there are several techniques. I do recommend you look at Peter's Software and his LASsie Product for a bolt-in system. It's polished and powerful.


Role Based Security: Cycle thru controls and set properties


Once you have security and global variables to set who logged in, here's a quick way to lock form controls:


The code below enables/disables controls, but you can change it for other properties and call several of these when needed. 


Predicate: You've got some method of determining who's logged in and/or their security level.


In the forms you wish to control, put marker text in the Tag property to tell the function what to do. In the example below, the word lock is used, but you could use several with simple enhancements to the functions. For example, some controls could have the word Lock, and some the word Vis (for visible).


Below are two functions, the first is called on the main form's OnOpen event. Calling would be something like:


If tempvar!userlevel < 10 then call LockControls(me.name)


If the main form has a subform, call the second function similarly from the subform's OnOpen event with something like:


If tempvar!userlevel < 10 then call LockControlsSub(me.parent.name, me.name)

Public Sub LockControls(frmName As String) On Error GoTo HandleErr

Dim cntrl As control

For Each cntrl In Forms(frmName)     If cntrl.Tag = "Lock" Then         cntrl.enabled = False     End If Next

ExitHere:     Exit Sub HandleErr: Select Case Err.Number     Case Else         Debug.Print "Error " & Err.Number & ": " & Err.Description & " BasControlManipulation.LockControls"     End Select End Sub

___________________________________________________________________________________________________________

Public Sub LockControlsSub(frmName As String, subfrmName As String) On Error GoTo HandleErr

Dim cntrl As control

For Each cntrl In Forms(frmName)(subfrmName).Form     If cntrl.Tag = "Lock" Then         cntrl.enabled = False     End If Next

ExitHere:    Exit Sub HandleErr: Select Case Err.Number     Case Else         Debug.Print "Error " & Err.Number & ": " & Err.Description & " BasControlManipulation.LockControlsSub"     End Select End Sub


#security #2factorauthentication #passwords

12 views0 comments