6.9. Database Encryption with DAO

Access 2007 now supports standard encryption algorithms that are included with Windows. To maintain backward compatibility with previous versions of Access, this feature is only available for the new file formats. Database encryption combines two features that have existed in Jet for some time: database passwords and encoding. You can decrypt the database by removing the database password.

You can set the database password using DAO in several ways:

  • Call the NewPassword method of the Database object.

  • Compact an existing database and include a password using the CompactDatabase method.

  • Create a new database using the CreateDatabase method and specify the password.

Using ActiveX Data Objects (ADO), you can run a query that calls ALTER DATABASE PASSWORD, but this does not work from DAO.

6.9.1. Setting the Database Password

Let's look at the three ways you can set a database password using DAO:

  • Using the NewPassword method

  • Compacting a database

  • Creating a new database

Setting the database password requires that the database is opened exclusively.

6.9.1.1. Using the NewPassword Method

To change the password for the current database without creating a new database, you can use the NewPassword method on the Database object like this:

Sub ChangePassword(strOldPassword As String, strNewPassword As String) 'Possible errors Const ERR_DB_OPENED_SHARED As Long = 3621 Const ERR_INVALID_PASSWORD As Long = 3031 On Error GoTo ChangePasswordErrors Dim dbs As DAO.Database ...

Get Access™ 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.