Adding users to groups using DAO

G

Guest

Hi.
Wanting to add users to security groups, I purchased a book from wrox that
gives me the following DAO function:

Public Function CreatuserAccount(strUserName As String, _
strPID As String, strPassword As String)

Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)

On Error GoTo CreatuserAccountErr

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreatuserAccountErr:
Set usr = Nothing
Set wrk = Nothing

End Function

This code looks great. But, I don't know how to use it. I created a module
and put the function in. But, what's the next step? Any help would be
appreciated. Thank you.
 
S

Scott McDaniel

Hi.
Wanting to add users to security groups, I purchased a book from wrox that
gives me the following DAO function:

Public Function CreatuserAccount(strUserName As String, _
strPID As String, strPassword As String)

Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)

On Error GoTo CreatuserAccountErr

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreatuserAccountErr:
Set usr = Nothing
Set wrk = Nothing

End Function

This code looks great. But, I don't know how to use it. I created a module
and put the function in. But, what's the next step? Any help would be
appreciated. Thank you.

The next step would be to build a form that would allow you to input the information, then call your function from that
form (say in the Click event of a button). So build a form (frmSecurity), add 3 textboxes (txtUserName, txtPID,
txtPassword), and add a button (cmdSave). In the Click event of cmdSave, add this code:

CreateUserAccount Me.txtUserName, Me.txtPID, Me.txtPassword

Note also that a Function should always return a value; in this case, your function could return a Boolean, indicatin
success or failure ... so alter your code like this:

Public Function CreatuserAccount(strUserName As String, _
strPID As String, strPassword As String)

Dim wrk As DAO.Workspace
Dim usr As DAO.User

Set wrk = DBEngine(0)

On Error GoTo CreatuserAccountErr

Set usr = wrk.CreateUser(strUserName, strPID, strPassword)
wrk.Users.Append usr

CreateUserAccount = True

CreateUserAccount_Exit:
Set usr = Nothing
Set wrk = Nothing

CreatuserAccountErr:
<error code here>
CreateUserAccount = False
Resume CreateUserAccount_Exit

End Function

Now you can call your function like this:

If Not CreateUserAccount( Me.txtUserName, Me.txtPID, Me.txtPassword) Then
MsgBox "There was a problem creating the user.", vbOkOnly
Else
MsgBox Me.txtUserName & " was added to the workgroup file"
End If


Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G

Guest

Dear Scott,

Thank you very much. I really appreciate that you took an extra step to
write additional code that would verify the success. My users will
appreciate your thoughtfulness and your name will be cited in my database (if
you don't mind, of course).
 

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