如何通过代码更改数据库密码-杜小杰
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


如何通过代码更改数据库密码

发表时间:2011/3/11 评论(0) 浏览(6624)  评论 | 加入收藏 | 复制
   
摘 要:设置数据库密码
下面的 ADO 示例假定当前数据库中包含一个空数据库密码。(要以独占方式打开数据库)

注:  引用确保已选中 Microsoft ActiveX 数据对象 2.x 库 复选框。

创建一个模块,键入以下代码

正 文:

Option Explicit
Sub ADOAddPW()
   Dim cn As ADODB.Connection
   Dim newpassword As String
   Dim NotValid As Integer
   Dim sqlExecStr As String

   Set cn = CurrentProject.Connection
   On Error Resume Next

   ' Test to see if the database is open exclusively.
   If cn.Mode <> 12 Then
      MsgBox "Your database is not opened exclusively", vbCritical
      Exit Sub
   End If
    
RetryPassword:
   newpassword = InputBox("Please enter new database password", "Database Password" _
        , "New Database Password")

' Select case for inputbox.
  
   Select Case newpassword
  
   ' Case where the cancel button was pressed.
  
    Case "New Database Password"
        MsgBox "No Database password set"
        Exit Sub
       
   ' Case where the OK button was pressed without entering data.
  
    Case ""
       NotValid = MsgBox("You have not entered a valid password, or clicked the cancel button" & Chr(10) & Chr(13) & _
       "Do you want to change the database password?", vbCritical + vbYesNo)
       If NotValid = 6 Then
        GoTo RetryPassword
       Else
        Exit Sub
       End If
      
    'If any data is entered other than the default value.
   
    Case Else
       sqlExecStr = "Alter Database Password " & newpassword & "``"
       CurrentProject.Connection.Execute sqlExecStr
       MsgBox "Database password has been set"
                    
    End Select
     
End Sub

若要测试此函数,单击 运行 菜单上的 运行子过程/用户窗体。
关闭并重新打开数据库。请注意系统将提示您输入数据库密码。

Access软件网交流QQ群(群号:198465573)
 
 相关文章
【Access扫盲知识】access菜鸟恼火问题之带密码的文件怎么...  【麥田  2013/2/28】
在平台上解决用户修改密码过于简单的问题?  【李锋  2013/4/6】
【Access源码示例】输入密码才能有权限编辑数据,用密码控制新增...  【麥田  2013/4/10】
【Access源码】记住密码及自动登录示例  【漏蛧尐魚℡  2013/4/11】
【Access源码】自动登录及记住密码登录窗口示例  【漏蛧尐魚℡  2013/5/16】
常见问答
技术分类
相关资源
文章搜索
关于作者

杜小杰

文章分类

文章存档

友情链接