首页 > 解决方案 > 以编程方式测试 MS Access 数据库是否已加密

问题描述

我正在努力自动发布我们的 MS Access 应用程序的新版本。

我们无法发送未加密的数据库,因此我想添加一个测试,该测试将显示一个带有数据库未加密警告的 msgbox。

有人知道我可以测试的对象/属性吗?

标签: vbams-access

解决方案


您可以通过创建用户定义的函数来测试它。在模块或表单模块中编写以下函数。

Public Function IsDbSecured(ByVal sDb As String) As Boolean
    On Error GoTo Error_Handler
    Dim oAccess         As Access.Application
 
    Set oAccess = CreateObject("Access.Application")
    oAccess.Visible = False
 
    'If an error occurs below, the db is password protected
    oAccess.DBEngine.OpenDatabase sDb, False
 
Error_Handler_Exit:
    On Error Resume Next
    oAccess.Quit acQuitSaveNone
    Set oAccess = Nothing
    Exit Function
 
Error_Handler:
    If Err.Number = 3031 Then
        IsDbSecured = True
         Else
        MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description, vbCritical, "Error"
    End If
    Resume Error_Handler_Exit
End Function

然后像这样从按钮或您想要的方式调用它。

Private Sub Command1_Click()
    Dim fileTestPath As String

    'Change your database path here.
    fileTestPath = "C:\Users\Harun.Rashid\Documents\Test_Close.accdb"

    If IsDbSecured(fileTestPath) = True Then
        MsgBox "Your database is encrypted by password"
    End If
    
End Sub

在此处输入图像描述


推荐阅读