我是否可以使用VBA将用户添加到Access,而不是使用内置的用户和组帐户框?

时间:2009-09-26 02:55:05

标签: ms-access

在创建访问数据库时,我想尽可能地将其作为白痴证明。这意味着,我不希望客户端必须使用访问组件;我宁愿拥有自己的表单,只需要用户名和密码,然后自动将其添加到正确的组中。

我以为我有一些可行的代码:

Dim usr as User 
set usr = new User

usr.Name="Foo"
'set other properties'
DBEngine.Workspace(0).Users.Append(usr)

但它告诉我不支持该操作。有没有其他方法可以将新用户插入安全文件中?

4 个答案:

答案 0 :(得分:5)

使用DDL创建用户名为“fred”,密码为“pword”:

CurrentProject.Connection.Execute "CREATE USER fred pword;"

将fred添加到用户和管理员组:

CurrentProject.Connection.Execute "ADD USER fred TO Users;"
CurrentProject.Connection.Execute "ADD USER fred TO Admins;"

数据定义语言的MSDN文档: http://msdn.microsoft.com/en-us/library/bb267262.aspx

您可以使用“ALTER USER ...”更改密码,使用“DROP USER ...”删除用户。

答案 1 :(得分:2)

您使用的是哪种访问版本。您是否只是尝试将新用户添加到组/新组中。

我在Access 2003上找到了这个例子,它似乎工作得很好

Sub CreateUserX(ByRef strPassword As String)

   Dim wrkDefault As Workspace
   Dim usrNew As user
   Dim grpNew As Group
   Dim usrTemp As user
   Dim prpLoop As Property
   Dim grpLoop As Group

   Set wrkDefault = DBEngine.Workspaces(0)

   With wrkDefault

      ' Create and append new User.
      Set usrNew = .CreateUser("NewUser")
      usrNew.PID = "AAA123456789"
      usrNew.Password = strPassword
      .Users.Append usrNew

      ' Create and append new Group.
      Set grpNew = .CreateGroup("NewGroup", _
         "AAA123456789")
      .Groups.Append grpNew

      ' Make the user "NewUser" a member of the
      ' group "NewGroup" by creating and adding the
      ' appropriate User object to the group's Users
      ' collection.
      Set usrTemp = _
         .Groups("NewGroup").CreateUser("NewUser")
      .Groups("NewGroup").Users.Append usrTemp

      Debug.Print "Properties of " & usrNew.Name

      ' Enumerate the Properties collection of NewUser. The
      ' PID property is not readable.
      For Each prpLoop In usrNew.Properties
         On Error Resume Next
         If prpLoop <> "" Then Debug.Print "  " & _
            prpLoop.Name & " = " & prpLoop
         On Error GoTo 0
      Next prpLoop

      Debug.Print "Groups collection of " & usrNew.Name

      ' Enumerate the Groups collection of NewUser.
      For Each grpLoop In usrNew.Groups
         Debug.Print "  " & _
            grpLoop.Name
      Next grpLoop

      ' Delete the new User and Group objects because this
      ' is a demonstration.
      .Users.Delete "NewUser"
      .Groups.Delete "NewGroup"

   End With

End Sub

这会有帮助吗?

答案 2 :(得分:1)

这篇MSDN文章介绍了您要做的事情:

http://msdn.microsoft.com/en-us/library/aa190108(v=office.10).aspx

讽刺的是,其他三个答案都涵盖了其中一种方法:DDL,DAO和ADOX。

答案 3 :(得分:0)

经过大量的头发拉动和对缺乏文件的史诗般的战斗,这里是。您必须使用ADOX。将其链接到VBA编辑器的参考菜单中。

Dim cat as ADOX.Catalog
Dim user as ADOX.User

Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection

usr = new ADOX.User
usr.Name = "joe"
cat.Users.Append usr

' must change password on user after inserted '
cat.Users("joe").ChangePassword "", "pass"
cat.Users("joe").Groups.Append "Users" ' have to be in this to open database '
cat.Users("joe").Groups.Append "MyCustomGroup"

Set cat = Nothing
Set usr = Nothing
  • 还有其他具有连接对象的对象。它们不起作用。 CurrentProject.Connection是唯一适合我的。
  • 某些文档在将用户对象附加到用户目录之前会显示更改密码。那不行。
  • 各种文档似乎表明Users Collection可以执行此操作,但无法附加 - 它需要ADOX目录。