HandsOn Adding a User to a New Group

This hands-on requires that you have performed all the steps listed under Custom Project 17-1 and Hands-On 17-2.

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

Sub AddUser_ToNewGroup() Dim cat As ADOX.Catalog Dim conn As ADODB.Connection Dim strDB As String Dim strSysDb As String Dim strName As String

On Error GoTo ErrorHandle

Part II

strDB = "C:\BookProject\SpecialDb.mdb" strSysDb = "C:\BookProject\Security.mdw" strName = "Elite"

' 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 With cat

.ActiveConnection = conn ' Create a new group .Groups.Append strName ' Add an exising user to the new group .Users("PowerUser").Groups.Append strName End With

MsgBox "Successfully added PowerUser to " & strName & _ " group account."

ExitHere:

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

Set cat = Nothing

If Err.Number = -2147467259 Then

MsgBox strName & " account already exists."

Else

MsgBox Err.Description End If

Resume ExitHere End Sub

Figure 17-13: Running the procedure in Hands-On 17-3 creates the new group named Elite and appends the PowerUser account to the group.

Creating and Manipulating Databases with ADO

0 0

Post a comment