Search Tools Links Login

Create database user

The following function creates a user. You can execute it under any user you like. (Dror Dotan A')

Original Author: Newsgroup Posting

API Declarations

const ADMIN_USERNAME = "Admin"
const ADMIN_PASSWORD = "adminpass (or whatever)"
const SHOWICON_STOP = 16


Function CreateNewUser% (ByVal username$, ByVal password$, ByVal PID$)
  '- create a new user.
  '- username$ - name
  '- password$ - user password
  '- PID$ - PID of user
  Dim NewUser As User
  Dim admin_ws As WorkSpace
  '- check PID
  If (Len(PID$) < 4 Or Len(PID$) > 20) Then
    MsgBox "Invalid PID", SHOWICON_STOP
    CreateNewUser% = True
    Exit Function
  End If
  '- verify that user does not yet exist
  If (UserExist%(username$)) Then
    CreateNewUser% = True
    Exit Function
  End If
  '- open new workspace and database as admin
  Set admin_ws = dbEngine.CreateWorkspace("TempWorkSpace",
                     ADMIN_USER, ADMIN_PASSWORD)
  If (Err) Then
    '- failed opening workspace
    MsgBox "invalid administrator password", SHOWICON_STOP
    MsgBox "Error: " & Error$, SHOWICON_STOP, SystemName
    CreateNewUser% = True
    Exit Function
  End If
  On Error Resume Next
  '- create the new user
  Set NewUser = admin_ws.CreateUser(username$, PID$, password$)
  If (Err) Then
    MsgBox "Can't create new user.", SHOWICON_STOP
    MsgBox Error$, SHOWICON_STOP
    GoTo CreateNewUser_end
  End If
  '- add user to user list
  admin_ws.Users.Append NewUser
  '- add user to "Users" group
  Set NewUser = admin_ws.CreateUser(username$)
  admin_ws.Groups("Users").Users.Append NewUser
  CreateNewUser% = False
  On Error GoTo 0
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 180 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.