Article ID: 112106
Article Last Modified on 10/11/2006
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
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.Permissions
This example shows how to read the permissions for the group Supervisors on
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.Permissions
Note 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.
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_FULLACCESS
The 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
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 Sub
You can call this subroutine from within code, passing the group or user
name and the Permissions value you want to assign.
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:
Option Compare Database Option Explicit Global Const SUCCESS_SUCCESS = 0
' ****************************************
' FUNCTION: GetPermissions()
'
' Inputs: UserGrpName - name of a user or group account
' ObjClass - name of an object container
' ObjName - name of an object document
'
' Returns: Value of Permissions property or error number
' that was generated.
' ****************************************
Function GetPermissions& (UserGrpName$, ObjClass$, ObjName$)
On Error GoTo Err_GetPermissions
' Set DB to the current database, and set the DOC variable
' .. to the object specified in the arguments.
Dim DB As Database, DOC As Document
Set DB = DBEngine.Workspaces(0).Databases(0)
Set DOC = DB.Containers(ObjClass).Documents(ObjName)
' Set the UserName property of the document to the
' .. user or group you want to obtain the permissions for.
DOC.UserName = UserGrpName
' Get the permissions value.
GetPermissions = DOC.Permissions
Bye_GetPermissions:
Exit Function
Err_GetPermissions:
' If an error occurs, display the message and terminate the
' .. function, returning the error number.
MsgBox Err & " " & Error$
GetPermissions = Err
Resume Bye_GetPermissions
End Function
' ****************************************
' FUNCTION: SetPermissions()
'
' Inputs: UserGrpName - name of a user or group account
' ObjClass - name of an object container
' ObjName - name of an object document
' NewPerm - new Permissions value
'
' NewPerm will typically be set by adding together the constants
' predefined for the security options. For a list of the
' constants search Help on "Permissions Property."
'
' Returns: SUCCESS_SUCCESS or the error number that was generated.
' ****************************************
Function SetPermissions& (UserGrpName$, ObjClass$, ObjName$, _
NewPerm&)
On Error Goto Err_SetPermissions
' Set DB to the current database, and set the DOC variable
' to the object specified in the arguments.
Dim DB As Database, DOC As Document
Set DB = DBEngine.Workspaces(0).Databases(0)
Set DOC = DB.Containers(ObjClass).Documents(ObjName)
' Set the UserName property of the document to the
' .. user or group you want to assign the permissions for.
DOC.UserName = UserGrpName
' Set the permissions property to the value passed as
' .. an argument to the function.
DOC.Permissions = NewPerm
SetPermissions = SUCCESS_SUCCESS
Bye_SetPermissions:
Exit Function
Err_SetPermissions:
' If an error occurs, display the message and terminate the
' .. function, returning the error number.
MsgBox Err & " " & Error$
SetPermissions = Err
Resume Bye_SetPermissions
End Function
Dim ObjProp as Long
ObjProp = GetPermissions("User1","Tables","Table1")
This function call returns the permissions value for User1 on the table
object Table1.
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)
Keywords: kbhowto kbprogramming KB112106