Create user in code

G

Guest

I have got to be the worst programmer ever. Truthfully I'm not a programmer
at all, but I have to do this anyway and I need help. :(

The following code is supposed to create a new user and add that user to the
"Users" group:

Private Function AddUser(ByVal strUser As String, _
ByVal strPID As String, _
Optional ByVal strPwd As String) As Boolean
Dim catDB As ADOX.Catalog

On Error GoTo AddUser_Err

' Instantiate the Catalog object.
Set catDB = New ADOX.Catalog
With catDB
' Open Catalog object by using connection to the current
' database.
.ActiveConnection = CurrentProject.Connection
' Create new user account.
.Users.Append strUser, strPwd, strPID
' Append new user account to default Users group.
.Groups("Users").Users.Append strUser
End With

' Close Catalog object.
Set catDB = Nothing

AddUser = True

AddUser_Err:
Msgbox Err.Number & ":" & Err.Description
AddUser = False
End Function
*************End Code**************

I'm not sure how to use this code. From what I understand, I need to create
a form that has three unbound textboxes. One named strUser, another named
strPID and the last named strPwd. I then need to create a button which calls
the addUser function when clicked. The problem is, when I click the build
button for the onclick event of my button, it creats a 'Private Sub'. This
code is a 'Private Function', how do I get the button to run the function?

Additionally, during my attempts to do this, I've added the code in the
coding window and then tried to compile the db but I get an error message on
the line that says: .Users.Append strUser, strPwd, strPID
The error message says: Compile Error: Wrong number of arguments or invalid
property assignment.

I don't know what that means and that's pretty much the most important part
of the code. Take note that I have already added the Microsoft ADO Ext. 2.5
for DDL and Security Reference to the References window. Also, I am trying to
run this code while logged in as an admin in my secured.mdw workgroup file.
 
B

Brendan Reynolds

I would advice you not to do this. Using code that you don't understand can
store up big problems for the future. How are you going to maintain and
enhance it if you don't understand it? But something tells me that you won't
take that advice, so you can try the following instead. Just don't say I
didn't warn you when it all goes pear-shaped, OK? :)

Paste the code you were given into the form's module, outside of any
procedure, at the top of the module under the 'Option Compare Database' line
and the 'Option Explicit' line if it is there. (Alternatively, you could
paste it into a standard module - choose Module from the Insert menu in the
VBA editor - and change 'Private' to 'Public', then you can call it from
anywhere in your application.) Now you can call the function from the
command button's click event using the function name and passing the
arguments like so ...

If AddUser(Me!txtUserName, Me!txtPassword) = True Then
MsgBox "User successfully added"
Else
MsgBox "Unable to add user"
End If

If you're asking at this stage: "But wait a minute, what happened to the
third argument, strPID?" ... congratulations, that's a good question, we'll
make a programmer of you yet! :)

The answer is that the code you were given is wrong, the Append method of
the ADOX.Users collection takes only two arguments, the user and the
password. So you'll need to edit the code to remove any reference to that
'strPID' argument.

A better solution would be to use DAO instead, because DAO does allow you to
specify the PID, which can be important if the workgroup file is lost and
must be recreated. See 'CreateUser Method (DAO)' in the help file for
details.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
G

Guest

Brendan,

I finally got it working. Here's the working code that I'm using if you're
interested:

'Call the sCreateUser Function
If sCreateUser(Me!strUser, Me!strPID, Me!varPwd) = True Then
MsgBox "FSR successfully added"
Else
MsgBox "Unable to Add User " & Err.Number & ": " & Err.Description
End If

Public Function sCreateUser(ByVal strUser As String, ByVal _
strPID As String, Optional varPwd As Variant) As Integer
'-----------------------------------------------------------
' Create a new user and add them to the Users group
' Returns True on success, False if user already exists
'===========================================================
Dim db As Database
Dim ws As Workspace
Dim usr As User
Dim grpUsers As Group
Dim strSQL As String
' if the password isn't supplied, make sure you
' pass an empty string for the password argument
If IsMissing(varPwd) Then varPwd = ""
Set ws = DBEngine.Workspaces(0)
ws.Users.Refresh
On Error Resume Next
' check to see if user already exists by using inline
' error handling to trap any errors caused by setting
' a reference to a possibly non-existent user
strUser = ws.Users(strUser).Name
If Err.Number = 0 Then
MsgBox "The user you are trying to add already exists.", _
vbInformation, "Can't Add User"
sCreateUser = False
Else
' go ahead and create the user account
Set usr = ws.CreateUser(strUser, strPID, varPwd)
ws.Users.Append usr
ws.Users.Refresh
' now add the user to the "Users" and "Update Data Users" groups
Set grpUsers = ws.Groups("Users")
Set usr = grpUsers.CreateUser(strUser)
grpUsers.Users.Append usr
grpUsers.Users.Refresh
Set grpUsers = ws.Groups("Update Data Users")
Set usr = grpUsers.CreateUser(strUser)
grpUsers.Users.Append usr
grpUsers.Users.Refresh
sCreateUser = True
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top