组和用户追加、ChangePassword 方法示例 (VB)
此示例演示 组的 Append 方法,以及向系统添加新的 组 和新的 用户,用户 的 追加 方法。 新的 组 将追加到新 用户的 组 集合中。 因此,新的 用户 将添加到 组。 此外,ChangePassword 方法用于指定 用户 密码。
注意
如果要连接到支持 Windows 身份验证的数据源提供程序,则应指定 Trusted_Connection=yes 或 Integrated Security = SSPI,而不是连接字符串中的用户 ID 和密码信息。
' BeginGroupVB
Sub Main()
On Error GoTo GroupXError
Dim cat As ADOX.Catalog
Dim usrNew As ADOX.User
Dim usrLoop As ADOX.User
Dim grpLoop As ADOX.Group
Set cat = New ADOX.Catalog
cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source='Northwind.mdb';" & _
"jet oledb:system database=" & _
"'system.mdw'"
With cat
'Create and append new group with a string.
.Groups.Append "Accounting"
' Create and append new user with an object.
Set usrNew = New ADOX.User
usrNew.Name = "Pat Smith"
usrNew.ChangePassword "", "Password1"
.Users.Append usrNew
' Make the user Pat Smith a member of the
' Accounting group by creating and adding the
' appropriate Group object to the user's Groups
' collection. The same is accomplished if a User
' object representing Pat Smith is created and
' appended to the Accounting group Users collection
usrNew.Groups.Append "Accounting"
' Enumerate all User objects in the
' catalog's Users collection.
For Each usrLoop In .Users
Debug.Print " " & usrLoop.Name
Debug.Print " Belongs to these groups:"
' Enumerate all Group objects in each User
' object's Groups collection.
If usrLoop.Groups.Count <> 0 Then
For Each grpLoop In usrLoop.Groups
Debug.Print " " & grpLoop.Name
Next grpLoop
Else
Debug.Print " [None]"
End If
Next usrLoop
' Enumerate all Group objects in the default
' workspace's Groups collection.
For Each grpLoop In .Groups
Debug.Print " " & grpLoop.Name
Debug.Print " Has as its members:"
' Enumerate all User objects in each Group
' object's Users collection.
If grpLoop.Users.Count <> 0 Then
For Each usrLoop In grpLoop.Users
Debug.Print " " & usrLoop.Name
Next usrLoop
Else
Debug.Print " [None]"
End If
Next grpLoop
' Delete new User and Group objects because this
' is only a demonstration.
' These two line are commented out because the sub "OwnersX" uses
' the group "Accounting".
' .Users.Delete "Pat Smith"
' .Groups.Delete "Accounting"
End With
'Clean up
Set cat.ActiveConnection = Nothing
Set cat = Nothing
Set usrNew = Nothing
Exit Sub
GroupXError:
Set cat = Nothing
Set usrNew = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
' EndGroupVB
另请参阅
Append 方法(ADOX 组)
Append 方法(ADOX 用户)
目录对象 (ADOX)
ChangePassword 方法 (ADOX)
组对象 (ADOX)
组集合 (ADOX)
用户对象 (ADOX)
用户集合 (ADOX)