ACC: How to Use DAO to Assign or View PermissionsLast reviewed: August 29, 1997Article ID: Q112106 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. In Microsoft Access version 7.0 for Windows 95 and Microsoft Access version 2.0, programmers can assign permissions to database objects programmatically using data access objects (DAO). Securing a database involves several steps, including:
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications" manual. NOTE: Visual Basic for Applications (used in Microsoft Access version 7.0 for Windows 95) is called Access Basic in version 2.0.
MORE INFORMATIONTo assign permissions for an object, you must reference the UserName and Permissions properties of the object's document. All database objects can be treated as documents in a container. For example, given the DB variable assignment
Dim DB As Database Set DB = DBEngine.Workspaces(0).Databases(0)a table called Table1 can be referred to using DAO as follows:
DB.Containers("Tables").Documents("Table1")However, it is usually easier to assign and work with object variables as follows:
Dim DB as Database, DOC as Document Set DB = DBEngine.Workspaces(0).Databases(0) Set DOC = DB.Containers("Tables").Documents("Table1")You can directly reference the UserName and Permissions properties of the Table1 document like this:
UserNameVariable = DOC.UserName PermissionsVariable = DOC.Permissions -or- DOC.UserName = "MyUser" DOC.Permissions = DB_SEC_FULLACCESS Reading Permissions InformationTo read the permissions that a given user has on an object, set the UserName property of the document to the name of the user or group you want to inquire on, and then read the value of the Permissions property. The following example shows how to read the permissions for the user John on form MyMainForm:
Dim DB as Database, DOC as Document, HisPermissions as Long Set DB = DBEngine.Workspaces(0).Databases(0) Set DOC = DB.Containers("Forms").Documents("MyMainForm") DOC.UserName = "John" HisPermissions = DOC.PermissionsThis example shows how to read the permissions for the group Supervisors on the report SalarySummary:
Dim DB as Database, DOC as Document, GroupPermissions as Long Set DB = DBEngine.Workspaces(0).Databases(0) Set DOC = DB.Containers("Reports").Documents("SalarySummary") DOC.UserName = "Supervisors" GroupPermissions = DOC.PermissionsNote that whether you are inquiring on a group or a user, you still set the UserName property. There is no GroupName property for a document.
Assigning Permissions InformationTo assign permissions for a user on an object, set the UserName property of the document to the name of the user or group, and then set the Permissions property of the document to the appropriate value. Generally, you should use the predefined constants when you are assigning permissions. For a complete list of the predefined constants, search for "Constants, Intrinsic," and then "Security Constants" using the Microsoft Access Help menu. The following example shows how to assign Full permissions for the user Martha on the table Employees:
Dim DB as Database, DOC as Document Set DB = DBEngine.Workspaces(0).Databases(0) Set DOC = DB.Containers("Tables").Documents("Employees") DOC.UserName = "Martha" DOC.Permissions = DB_SEC_FULLACCESSThe next example shows how to assign Open/Run and Modify Design permissions for the group Developers on the form Customers:
Dim DB as Database, DOC as Document Set DB = DBEngine.Workspaces(0).Databases(0) Set DOC = DB.Containers("Forms").Documents("Customers") DOC.UserName = "Developers" DOC.Permissions = DB_SEC_FRMRPT_EXECUTE+DB_SEC_FRMRPT_WRITEDEF Methods to Programmatically View and Assign PermissionsMethod 1: This method uses a sample subroutine called AssignPerms to update all the objects in a database. To assign permissions for a user on all the objects in a database, you can move through all the documents in all the collections in the database. The following sample subroutine demonstrates one way of moving through all the documents in a database and changing the permissions on them:
Sub AssignPerms (GrpUsrName as String, NewPerm as Long) Dim DB as Database, I as Integer, J as Integer Set DB = DBEngine.Workspaces(0).Databases(0) For I = 0 to DB.Containers.Count - 1 For J = 0 to DB.Containers(I).Documents.Count - 1 DB.Containers(I).Documents(J).UserName = GrpUsrName DB.Containers(I).Documents(J).Permissions = NewPerm Next J Next I End SubYou can call this subroutine from within code, passing the group or user name and the Permissions value you want to assign. For example, to revoke all permissions on all objects from the Guests group, call AssignPerms as follows:
AssignPerms "Guests", DB_SEC_NOACCESSTo assign full permissions on all objects to the Managers account, call AssignPerms as follows:
AssignPerms "Managers", DB_SEC_FULLACCESSMethod 2: This method uses two sample functions, GetPermissions() and SetPermissions(), to display and assign the permissions for a user or group on the database object you specify. To create the sample functions, follow these steps:
The following example shows how to call the GetPermissions() function from another function:
Dim ObjProp as Long ObjProp = GetPermissions("User1","Tables","Table1")This function call returns the permissions value for User1 on the table object Table1. The next example shows how to call the GetPermissions() function from the ControlSource property of a control on a form or report with controls that contain the user name, object class, and object name that you want to list the permissions for:
ControlName: ObjProp ControlSource: =GetPermissions(Me!UsrName, Me!ObjClass, Me!ObjName)To assign full permissions for the Admins group to a table called MyTable, you can call the SetPermissions() function as follows:
Dim RETVAL as Long RETVAL = SetPermissions("Admins","Tables","MyTable", DB_SEC_FULLACCESS) REFERENCESFor more information about using DAO to create group and user accounts, and about how to assign users to group accounts, please see the following articles in the Microsoft Knowledge Base:
ARTICLE-ID: Q124240 TITLE : ACC2: Only Admins Group Members Can List Groups They Belong To ARTICLE-ID: Q112063 TITLE : ACC: How to Add a User to a Group with CreateUser Method Keywords : kbprg PgmHowTo MdlDao ScrtPerm Version : 2.0 7.0 Platform : WINDOWS Hardware : x86 Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |