Microsoft KB Archive/112063

= ACC: How to Add a User to a Group with CreateUser Method =

Article ID: 112063

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q112063



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

To add a user to an existing group with code, you must use the CreateUser method twice: once to create the new user for that Microsoft Access session, and again to create the user for the group.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications with Microsoft Access 2.0" manual.



MORE INFORMATION
The following sample function called CUser demonstrates how to create a user named MyName for the Users group:

  Create a module and type the following procedure:

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code Access Basic. Function CUser (uname As String, upin As String, upwd As String,_      gname As String) Dim u As User, w As WorkSpace, g As Group Set w = dbengine.workspaces(0)         'The default workspace. Set u = w.CreateUser(uname, upin, upwd) 'Create a user for the 'Access session. w.users.Append u                       'Add user to workspace. w.users.Refresh Set g = w.groups(gname) Set u = g.CreateUser(uname)            'Create a user for a                                                  group. g.users.Append u                       'Add new user to group. g.users.Refresh End Function  To test this function, type the following line in the Debug window (or the Immediate window in version 2.0), and then press ENTER:

? CUser ("MyName","1234","MyName","Users")

 On the Tools menu point to Security, and then click User And Group Permissions.

NOTE: In version 2.0, on the Security menu, click Permissions.

Note that the new user appears in the Users/Group Name box.

