Listing All User Accounts

The procedure in Hands-On 17-7 demonstrates how to retrieve the names of all defined security user accounts from the Users collection of the Catalog object.

©Hands-On 17-7: Listing All User Accounts

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

Sub List_Users()

Dim conn As ADODB.Connection Dim cat As ADOX.Catalog Dim myUser As New ADOX.User Dim strDB As String Dim strSysDb As String 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 For Each myUser In cat.Users Debug.Print myUser.Name


Set cat = Nothing conn.Close

Set conn = Nothing

MsgBox "Users are listed in the Immediate window." End Sub

Creating and Manipulating Databases with ADO

Figure 17-15: The names of existing security user accounts are written to the Immediate window by the List_Users procedure in Hands-On 17-7. In addition to the user accounts that you have defined, Access reveals the names of its two built-in users: Creator and Engine. To keep these built-in users from showing up in your users listing, use the following conditional statement:

If myUser.Name <>"Creator" And _ myUser.Name <> "Engine" Then Debug.Print myUser.Name

End If

0 0

Post a comment