How do I programmatically remove a known password from an Access DB?

后端 未结 2 1949
天命终不由人
天命终不由人 2021-01-06 12:34

For reasons beyond my control, I have to deal with a new Access MDB file that is downloaded, decrypted, and unzipped every month by an automated process I wrote. Despite the

2条回答
  •  死守一世寂寞
    2021-01-06 12:58

    The way to change the password programmatically is detailed here.

    Essentially, one needs to do the following:

    1. Open a connection to the database using ADO.NET
    2. Execute an alter statement on the database setting it's password to NULL as so:

      ALTER DATABASE PASSWORD [Your Password] NULL;

    3. Dispose the connection

    Sample code taken from the source:

    Private Function CreateDBPassword(ByVal Password As String, _
        ByVal Path As String) As Boolean
    Dim objConn as ADODB.Connection
    Dim strAlterPassword as String
    On Error GoTo CreateDBPassword_Err
    ' Create the SQL string to initialize a database password.
    strAlterPassword = "ALTER DATABASE PASSWORD [Your Password] NULL;"
    
    ' Open the unsecured database.
    Set objConn = New ADODB.Connection
    With objConn
        .Mode = adModeShareExclusive
        .Open "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
            "Source=[Your Path];" 
    
     ' Execute the SQL statement to secure the database.
     .Execute (strAlterPassword)
    End With
    
    ' Clean up objects.
    objConn.Close
    Set objConn = Nothing
    
    ' Return true if successful.
    CreateDBPassword = True
    
    CreateDBPassword_Err:
    Msgbox Err.Number & ":" & Err.Description
    CreateDBPassword = False 
    End Function
    

提交回复
热议问题