Setting User Permissions for a Database

To specify permissions for the database, specify an empty string (" ") as the name of the database:

cat.Users("PowerUser").SetPermissions " ", _ adPermObjDatabase, _ adAccessSet, adRightExclusive

The above statement gives the user named PowerUser the right to open the database exclusively.

©Hands-On 17-11: Setting User Permissions for a Database

1. Insert a new module and, in the module's Code window, enter the Set_UserDbPermissions procedure as shown below.

Sub Set_UserDbPermissions()

Dim conn As ADODB.Connection Dim cat As ADOX.Catalog Dim strDB As String Dim strSysDb As String

On Error GoTo ErrorHandle strDB = "C:\BookProject\SpecialDb.mdb" strSysDb = "C:\BookProject\Security.mdw"

' Open connection to the database using ' the specified system database Set conn = New ADODB.Connection With conn

.Provider = "Microsoft.Jet.OLEDB.4.0" .Properties("Jet OLEDB:System Database") = strSysDb .Properties("User ID") = "Developer" .Properties("Password") = "chapter17" .Open strDB End With

' Open the catalog

Set cat = New ADOX.Catalog cat.ActiveConnection = conn

' add a user account cat.Users.Append "PowerUser", "star"

' Set permissions for PowerUser cat.Users("PowerUser").SetPermissions " ", adPermObjDatabase, _

adAccessSet, adRightExclusive MsgBox "PowerUser has been granted permission to " & vbCrLf & _ "open the database exclusively."

ExitHere:

Set cat = Nothing conn.Close Set conn = Nothing Exit Sub

Part II

ErrorHandle:

If Err.Number = -2147467259 Then

' because PowerUser user already exists ' we ignore this statement Resume Next

Else

MsgBox Err.Description Resume ExitHere End If End Sub

Figure 17-20: The following settings are found in the User and Group Permissions window for the SpecialDb database after running the Set_UserDbPermissions procedure in Hands-On 17-11.

0 0

Post a comment