Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

User and Group Check

In a secured Database; basic access rights to Objects like Tables, Forms, Queries and Reports are defined for specific Workgroups/Users as a one time exercise. This takes effect automatically when a User belongs to a particular Workgroup accesses the Database Objects.

For example: Assume that the Employees Table is set with only Read Data permission for GroupA Workgroup.  When a User belongs to GroupA opens the Employees Form with the Employees Table as Record Source or opens the Table directly, he cannot execute Update/Insert/Delete Data operations on the Table. 

But, if we want to make this scenario little more flexible like allowing to Update data then this can be enabled on the User and Group Permissions Control under Security of Tools Menu.

In this case all Users belongs to GroupA Workgroup can Edit and Update all Data Fields of the Employees Table.  Normally, Users are never allowed to open Tables directly but through Data Entry/Edit/Display Forms so that the Developer have more control on the Users.

When we assign Update Data permission all data fields can be modified/updated by Users.  But, if we want to prevent the Users from making changes to certain fields; it cannot be done through the normal security implementation method explained above.

This level of security can be implement only through Visual Basic Programs.  This method can be implemented in the following way:

  1. When the Employees Form is open by the User for normal operations we can get the User Name with the use of CurrentUser() Function.
  2. Next step is to check whether this User belongs to the GroupA Workgroup or not.
  3. If so then lock the BirthDate and HireDate fields so that the current user is prevented from making changes to these two fields.

We need two programs to try out this method:

  1. A Function to check and confirm whether the User Name passed to it belongs to a particular Workgroup, if so send a positive signal back to the calling program.
  2. If the User is identified as a member of the GroupA Workgroup then the BirthDate and HireDate data fields are locked on the Form through the Form_Load() Event Procedure so that the current user cannot edit the contents of these fields.
  3. If the User belongs to some other Workgroup then the above fields are unlocked and allowed to edit/update.

To try this out:

  1. Import the Employees Table and Employees Form from the sample database C:\Program Files\Microsoft Office\Office11\Samples\Northwind.mdb.
  2. Open and existing Standard VBA Module or create a new one.
  3. Copy and paste the following Visual Basic Code into the Module and save it:
    Public Function UserGroupCheck(ByVal strGroupName As String, ByVal strUserName As String) As Boolean
    Dim WrkSpc As Workspace, Usr As User
    
    On Error GoTo UserGroupCheck_Err
    
    Set WrkSpc = DBEngine.Workspaces(0)
    
    For Each Usr In WrkSpc.Groups(strGroupName).Users
    If Usr.Name = strUserName Then
        UserGroupCheck = True
        Exit For
    Else
        UserGroupCheck = False
    End If
    
    Next
    
    UserGroupCheck_Exit:
    Exit Function
    
    UserGroupCheck_Err:
    MsgBox Err.Description, , "UserGroupCheck_Err"
    Resume UserGroupCheck_Exit
    
    End Function
  4. Open the Employees Form in Design View.
  5. Display the Form's VBA Module (View - -> Code).
  6. Copy and paste the following Code into the VBA Module and save the Form:
    Private Sub Form_Load()
    Dim strUser As String, strGroup As String, boolFlag As Boolean
    
    strUser = CurrentUser
    strGroup = "GroupA" 'replace the GroupA value with your own test Group Name
    boolFlag = UserGroupCheck(strGroup, strUser)
    
    If boolFlag Then
       Me.BirthDate.Locked = True
       Me.HireDate.Locked = True
    Else
       Me.BirthDate.Locked = False
       Me.HireDate.Locked = False
    End If
    
    End Sub
  7. Open the Form in normal View.
  8. Try to change the existing values in the BirthDate and HireDate Fields.

If the Current User name belongs to the Workgroup name you have assigned to the strGroup Variable then the Birthdate and HireDate fields will be in locked state.

Tip: Even if your database is not implemented with Microsoft Access Security you can test these programs. Assign the value Admins to the strGroup variable in the above Subroutine. By default you will be logged in as Admin User, as a member of the Admins Workgroup. This will lock both the above test fields from editing when the Employees Form is open.

Technorati Tags:
Share:

No comments:

Post a Comment

Translate



PageRank
Your email address:

Delivered by FeedBurner

Search

Infolinks Text Ads


Blogs Directory

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Labels

Forms How Tos Functions MS-Access Security Reports Animations msaccess animation msaccess forms Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Queries External Links msaccess reports msaccess tips Menus and Toolbars Accesstips MsaccessLinks Process Controls Art Work Downloads msaccess How Tos Graph Charts msaccessQuery List Boxes Command Buttons Emails and Alerts Query Combo Boxes Custom Wizards ms-access functions msaccess graphs msaccess reporttricks msaccessprocess security advanced Access Security DOS Commands Data Macros Menus Property Report Top Values VBA msaccess email msaccess menus progressmeter Access2007 Array Auto-Number Command Button Custom Functions Join Microsoft Numbering System Records Security Split SubForm Table Utility Workgroup database msaccess functions msaccess wizards Access2003 Accounting Year Action Animation Attachment Binary Numbers Budgeting Calculation ChDir Color Palette Conditional Formatting Controls Copy Data Filtering Data Type Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export Expression External Field Type Fields Filter Form Form Instances Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Methods Monitoring Object Reference Objects Octal Numbers Operating System Paste Primary-Key Product Rank Reading Rich Text Sequence SetFocus Summary Tab-Page Time Difference Union Query User Users Variables Water-Mark Word automatically commands function hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar tutorial updating upload vba code

Featured Post

DIR Getting File Names From Folder

We all know Dir() Function from the time of Windows DOS Operating System.  This is the first Command introduced to those who sit on a Perso...

Labels

Blog Archive

Recent Posts