Securing Users and Groups

The Users collection contains all users defined in the current workgroup information file. Similarly, the Groups collection contains all groups. By manipulating these collections, you define and control the security accounts that Microsoft Jet uses. The Groups and Users collections are interesting in that they are self-referencing. You can see from the DAO hierarchy that the Users collection contains a Groups collection, and the Groups collection contains a Users collection. Using this structure, you can easily find which users belong to which groups, and which groups contain which users.

There are a few things to keep in mind when you’re working with users and groups:

The following example illustrates these points. The procedure creates a new user, appends it to the Users group, then appends it to the Admins group.

Sub AddUserToAdmins(strWorkgroup As String, strUser As String, _
		strPID As String, strPwd As String)
	Dim wrk As Workspace
	Dim usr As User, grp As Group

	' Specify which workgroup information file to use.
	DBEngine.SystemDB = strWorkgroup

	Set wrk = DBEngine(0)
	' Create new user in Users collection of default Workspace object.
	Set usr = wrk.CreateUser(strUser, strPID, strPwd)
	wrk.Users.Append usr
	
	' Create user in Users collection of Users group.
	Set grp = wrk.Groups("Users")
	Set usr = wrk.CreateUser(strUser)
	grp.Users.Append usr
	
	' Create user in Users collection of Admins group.
	Set grp = wrk.Groups("Admins")
	Set usr = grp.CreateUser(strUser)
	grp.Users.Append usr
	grp.Users.Refresh
End Sub

Keep the following additional points in mind when working with User and Group objects: