Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Group Account Permissions with VBA


We have learned how to remove all permissions from all Objects for Users Group Account as all Users belongs to this default Group Account and inherits permissions, if available, besides other Group Accounts each user belongs to. If you have not gone through the earlier Articles on Microsoft Access Security with VBA, you may take a look at the following links before continuing:

Nature of Permission Assignment

Last week we were talking about removing all permissions for a particular Group Account. Now, we will learn how to assign different set of permissions for each set of Objects: Tables, Queries, Formsand other Objects for a particular Group Account. When done, all Users belongs to this particular Group Account will be allowed restricted access to all Objects.

For Tables/Queries the Users cannot make design changes but they can view the Table Structure or Query Design. Users can Add/Edit/Delete Recordsto Tables.

Users can Open Run Forms and Reportsbut cannot view or make Design Changes.

Macroscan be run but cannot view or make change to the Design.

Automating Object Level Permission Settings

The VBA Routine given below is run for an external Database and sets permissions for the given User-Group Account in the active Workgroup Information File.

Remember, the User-Names, Passwords and User-Group Accounts are maintained in the Workgroup Information File but permission settings stays with the Databases. So, you can say Microsoft Access Security is a two part combination Lock/Key.

  1. Copy and paste the following VBA Code into a Standard Module of your Database.
    Public Function SetPermission2Grp(ByVal DatabaseName As String, ByVal GroupName As String) As Boolean
    'Author : a.p.r. pillai
    'Date   : March-2010
    'Purpose: To Assign Security Permissions to Objects
    '       : for a particular Security Group Account
    '       : in an external Database.
    'Note   : The Security Group Account must be present
    '       : in the active Workgroup Information File
    '       :'Remarks: All Rights Reserved by www.msaccesstips.com
    Dim wsp As Workspace, db As Database, ctr As Container, doc As Document
    Dim ctrName As String, docName As String
    Dim L4 As String
    Const OBJSFULL = &HD01FE
    Const dbOpenRun = 2
    Const FrmRptOpenRun = 256
    Const MacOpenRun = 8
    Const TblQryExcludingModifyAdmin = 244 'All permissions Exluding Modify & Administr
    'Assign Permissions to Group Account
    On Error GoTo SetPermission2Grp_Err
    Set wsp = DBEngine.Workspaces(0)
    Set db = wsp.OpenDatabase(DatabaseName)
    For Each ctr In db.Containers
    ctrName = ctr.Name
    ctr.UserName = GroupName
    Select Case ctrName
    Case "Databases"
    For Each doc In ctr.Documents
             docName = doc.Name
             doc.UserName = GroupName
             Select Case docName
               Case "MSysDb"
                 'Set Open/Run Permissions to Database Object
                  doc.Permissions = doc.Permissions Or dbOpenRun
             End Select
          Next doc
       Case "Forms"
        'Set Open/Run Permissions to Forms Container
          ctr.Permissions = ctr.Permissions Or FrmRptOpenRun
          ctr.Inherit = True
          For Each doc In ctr.Documents
             docName = doc.Name
             doc.UserName = GroupName
             'Set Open/Run Permissions to Each Form
            doc.Permissions = doc.Permissions Or FrmRptOpenRun
          Next doc
       Case "Reports"
        'Set Open/Run Permissions to Reports Container
          ctr.Permissions = ctr.Permissions Or FrmRptOpenRun
          ctr.Inherit = True
          For Each doc In ctr.Documents
             docName = doc.Name
             doc.UserName = GroupName
             'Set Open/Run Permissions to Each Report
             doc.Permissions = doc.Permissions Or FrmRptOpenRun
          Next doc
       Case "Scripts"
         'Set Open/Run Permissions to Macros Container
          ctr.Permissions = ctr.Permissions Or MacOpenRun
          ctr.Inherit = True
          For Each doc In ctr.Documents
             docName = doc.Name
            doc.UserName = GroupName
             'Set Open/Run Permissions to Each Macro
             doc.Permissions = doc.Permissions Or MacOpenRun
          Next doc
       Case "Tables"
          '1. Assigns Full Permissions to Tables & Queries
          ctr.Permissions = ctr.Permissions Or OBJSFULL
          ctr.Inherit = True
          For Each doc In ctr.Documents
            docName = doc.Name
            doc.UserName = GroupName
             L4 = Left$(docName, 4)
           'Exclude System Objects
            If L4 = "MSys" Or L4 = "~sq_" Then
              GoTo nextloop
            End If
            '2. Remove Modify and Administrator permissions
            doc.Permissions = doc.Permissions Or TblQryExcludingModifyAdmin
          Next doc
      End Select
     ctrSetPermission2Grp = false
    Set db = Nothing
    Set wsp = Nothing
    Exit Function
    MsgBox Err & ": " & Err.Description, , "SetPermission2Grp"
    SetPermission2Grp = True
    Resume SetPermission2Grp_Exit
    End Function

  2. To test the Program create a copy of any Database and find a Group Account that is not assigned with permissions for the target database.
  3. Press Alt+F11 to open the VBA Editing Window, if you have already closed it.
  4. Press Ctrl+G to open the Debug Window.
  5. Type the following Statement in the Debug Window and press Enter Key to Run the Code and assign permissions to the selected Group Account in the Test Database: SetPermission2Grp "C:\My Documents\TestData.mdb", "FAGRP"
  6. Replace the Pathname of the Database and the Security Group Account Name with your own.
  7. After running the Code open the Test Database and check the Permission Settings of the Group Account you have specified as parameter.

Note: You may Run the Program from a Command Button Click after assigning the Database Pathname and Security Group Account Name in Text Boxes.


1 comment:

  1. My cousin recommended this blog and she was totally right keep up the fantastic work!


Comments subject to moderation before publishing.


Subscribe in a reader
Your email address:

Delivered by FeedBurner


Popular Posts

Empty Gadget

Blog Archive

Powered by Blogger.

Follow by Email


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

Featured Post

Word Mail-Merge with Ms-Access Table

Introduction. Form Letters. Form Letters are prepared from a standard Microsoft Word template merged with addressee related information fill...


Blog Archive

Recent Posts