MS Access – VBA – Change Database Password

Ever wanted to change the password of a Microsoft Access database with just a click of a button, rather than manually opening the file and navigating through menus? Or perhaps you wish to create/use your own database Form to allow users to update the database password with ease through your application.

It’s not only possible, but can be implemented quite easily in Access!
 

Change the Current Database’s Database Password

The simplest scenario is changing the active database’s database password and it can be accomplished using a procedure such as:

'---------------------------------------------------------------------------------------
' Procedure : Set_CurrentDBPassword
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Change the current database's database password.
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
' IMPORTANTS: This function will only work if the current database is opened in
'               Exclusive mode!
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sOldPwd   : The existing database password
' sNewPwd   : The new password to replace the sCurrentPassword with
'
' Usage:
' ~~~~~~
' Call Set_CurrentDBPassword("Admin", "root")
'   Returns -> True/False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2012-Sep-10                 Initial Release
'---------------------------------------------------------------------------------------
Function Set_CurrentDBPassword(ByVal sOldPwd As String, _
                          ByVal sNewPwd As String) As Boolean
    Dim oDb                   As DAO.Database

    On Error GoTo Error_Handler

    'Password can be a maximum of 20 characters long
    If Len(sNewPwd) > 20 Then
        MsgBox "Your password is too long and must be 20 characters or less." & _
               "  Please try again with a new password", vbCritical + vbOKOnly
        GoTo Error_Handler_Exit
    End If

    Set oDb = CurrentDb
    oDb.newPassword sOldPwd, sNewPwd
    
    Set_CurrentDBPassword = True
    
Error_Handler_Exit:
    On Error Resume Next
    Set oDb = Nothing
    Exit Function

Error_Handler:
    If Err.Number = 3621 Then
        MsgBox "The database must be opened in Exclusive mode to change its paswword." & vbCrLf & vbCrLf & _
               "Please close the database and reopen it in Exclusive mode and try again.", _
               vbCritical Or vbOKOnly, "Operation Aboarted"
    Else
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Source: Set_CurrentDBPassword" & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function

Remember though, that to be able to alter the current database’s database password you must first have it opened in Exclusive mode!

Then to change the password you could simply call the procedure by doing:

Call Set_CurrentDBPassword("Admin", "root")

where ‘Admin’ is the current password and you want to update it to ‘root’.
 

Change Another Database’s Database Password

On the other hand, if you would like to implement, change or remove the database password of another database, then, in such a case you would use a procedure such as:

'---------------------------------------------------------------------------------------
' Procedure : Set_DBPassword
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Change the password of any given database
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sDBName   : Full path and file name with extension of the database to modify the pwd of
' sOldPwd   : Existing database pwd - use "" if db is unprotect
' sNewPwd   : New pwd to assign - Optional, leave out if you which to remove the
'             existing pwd
'
' Usage:
' ~~~~~~
' Set a pwd on a db which never had one
' Set_DBPassword "C:\Users\Daniel\Desktop\db1.accdb", "", "test"
'
' Clear the password on a db which previous had one
' Set_DBPassword "C:\Users\Daniel\Desktop\db1.accdb", "test", "" 'Clear the password
'
' Change the pwd of a pwd protected db
' Set_DBPassword "C:\Users\Daniel\Desktop\db1.accdb", "test", "test2"
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-Sep-10                 Initial Release
'---------------------------------------------------------------------------------------
Public Sub Set_DBPassword(sDBName As String, sOldPwd As String, Optional sNewPwd As String = "")
    On Error GoTo Error_Handler
    Dim db              As DAO.Database

    'Password can be a maximum of 20 characters long
    If Len(sNewPwd) > 20 Then
        MsgBox "Your password is too long and must be 20 characters or less." & _
               "  Please try again with a new password", vbCritical + vbOKOnly
        GoTo Error_Handler_Exit
    End If

    'Could verify pwd strength
    'Could verify ascii characters

    Set db = OpenDatabase(sDBName, True, False, ";PWD=" & sOldPwd)    'open the database in exclusive mode
    db.NewPassword sOldPwd, sNewPwd    'change the password

Error_Handler_Exit:
    On Error Resume Next
    db.Close    'close the database
    Set db = Nothing
    Exit Sub

Error_Handler:
    'err 3704 - not able to open exclusively at this time, someone using the db
    'err 3031 - sOldPwd supplied was incorrect
    'err 3024 - couldn't locate the database file
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: Set_DBPassword" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

and then you could call it by doing:

Create a Password on a Database without a Password

Call Set_DBPassword("C:\Users\Daniel\Desktop\db1.accdb", "", "Admin")

Update/Change the Password on a Database with a Password

Call Set_DBPassword("C:\Users\Daniel\Desktop\db1.accdb", "Admin", "root")

Remove the Password on a Database with a Password

Call Set_DBPassword("C:\Users\Daniel\Desktop\db1.accdb", "root", "")

 

Database Password Change Considerations

After altering the password, you must recreate your linked tables in the front-end database. This is necessary because the existing links still contain the old password information and will no longer function correctly.

So you will either need to:

  • Delete the existing linked tables and then recreate them using code or the External Data -> Access wizard.
  • Use code to update the connection strings of all the linked tables, so as to update the associated password.

 

Page History

Date Summary of Changes
2012-09-10 Initial Release
2025-02-26 Added Change the Current Database’s Database Password section to the article.

6 responses on “MS Access – VBA – Change Database Password

  1. Diana van den Berg

    Thank you, Daniel, very much for the above. Could it be used in a utilities database to change the password for a Word document.?

    1. Daniel Pineault Post author

      No, this only applies to MS Access databases and will not work on Word documents, Excel workbook, …

  2. Bill Benson

    This fails under the “no new password” scenario, for my back-end database. I get a message “not a valid password”.

    This is Ac2013, the backend database is a .ACCDB

    1. Daniel Pineault Post author

      I have no issue and I’ve tested with 2013. Without more information, knowing exactly how you are calling the routine it is hard to say where the issue lies.