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

Last reviewed: January 22, 1998
Article ID: Q112063
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97

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:

  1. 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
    
    

  2. 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")
    

  3. 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.

REFERENCES

For more information about the CreateUser method, search the Help Index for "CreateUser" or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbprg PgmObj ScrtUsr
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 22, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.