会员登录 - 用户注册 - 网站地图 Office中国(office-cn.net),专业Office论坛
当前位置:主页 > 技巧 > Access技巧 > 模块函数VBA > 正文

如何用VBA更改数据库密码

时间:2005-02-05 12:16 来源:gab2001uk 作者:未知 阅读:


使用DAO方式改变密码

Private Sub cmdChangeDAO_Click()
    Dim strNewPassword As String
    
    'Check if a database is open
    If fDC Then 'using data control
        'Get new password
        strNewPassword = InputBox("Please type in new password. Leave blank to remove password protection")
        'Change password
        Data1.Database.NewPassword strPassword, strNewPassword
    Else
        If strSource = "" Then
            MsgBox "No current database open"
        Else
            'Get new password
            strNewPassword = InputBox("Please type in new password. Leave blank to remove password protection")
            'Change password
            db.NewPassword strPassword, strNewPassword
        End If
    End If
    'Update variable
    strPassword = strNewPassword
    WriteCaptions fDAO
End Sub

使用ADO方式改变密码

Private Sub cmdChangeADO_Click()
    Dim strNewPassword As String
    Dim strsql As String
    
    If fDC Then 'using data control
        'cannot change password from ADODC
        MsgBox "You have opened the database with ADODC data control. This control is not capable of changing the database password." & vbCrLf & "Use ADO Code connection string to achieve this."
    Else
        'Check if a database is open
        If strSource = "" Then
            MsgBox "No current database open"
        Else
            'Get new password
            strNewPassword = InputBox("Please type in new password. Leave blank to remove password protection")
            'Change password
            If strNewPassword = "" Then 'Remove password
                strsql = "ALTER Database Password `` " & strPassword
            Else
                If strPassword = "" Then 'no current password
                     strsql = "ALTER Database Password " & strNewPassword & " ``"
                Else 'straight forward change
                    strsql = "ALTER Database Password " & strNewPassword & " " & strPassword
                End If
            End If
            cnn.Execute strsql
        End If
    End If
    'Update variable
    strPassword = strNewPassword
    WriteCaptions fDAO
End Sub

 

(责任编辑:admin)

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价: