Working with the DAOPermission Enumeration

In DAO, the permission object is nothing more than a long integer which stores a set of bits representing the various permission settings. Multiple permission types can be set for a single permission object by OR-ing the permissions types together. Using the OR logic to mask the bits together preserves all "1" bits in the both of the enumeration values being operated on, producing a cumulative effect for the permissions. Do not confuse OR logic with AND logic, which only preserves the "1" bits common to both values, a common mistake developers can make. The following is an example function which will combine two permission objects into one permission object, preserving both permission settings applied:

Public Function CombinePermissions( _ lnglnitialPermissions As Long, _ lngPermissionToCombine As Long) As Long

' Combine the Permissions together

CombinePermissions = lnglnitialPermissions Or lngPermissionToCombine End Function

In contrast to combining permissions together, it is conceivable that a developer wants to write procedures to revoke permissions. This can be not by using the AND NOT operators for the permission to remove. In the case, the NOT operator flips the bits in the permission to be removed, and then AND logic removes the specific permission type. The following is an example of how a function could be created to remove a specific set of permissions form the permission value:

Public Function RevokePermissions( _ lnglnitialPermissions As Long, _ lngPermissionToRemove As Long) As Long

' Combine the Permissions together

RevokePermissions = lnglnitialPermissions And Not lngPermissionToRemove End Function

The ChangeOwnerDAO procedure is included with the permissions section because owners always have full permissions. Notice that it is only possible to change ownership on documents (that is, tables, forms, reports, pages, and macros), not on the database or the containers.

Public Sub ChangeOwnerDAO( _ strContainer As String, _ strDocumentName As String, _ strOwner As String)

' Define variables Dim db As Database Dim doc As Document

' Get the current database object Set db = CurrentDb()

' Get the Document object

Set doc = db.Containers(strContainer).Documents(strDocumentName)

' Change the Owner of the Document doc.Owner = strOwner

' Clean up

Set doc = Nothing db.Close

Set db = Nothing End Sub

0 0

Post a comment