Encrypting a Database Using Code
Chapter 27 showed how you can encrypt a database by using the user interface. If a database is not encrypted, it is not really secure because a savvy user can use a disk editor to view the data in the file. If you have distributed your application with the runtime version of Access, and you want to give your user the capability to encrypt the database, you must write ADO code to accomplish the encryption process. The code looks like this:Sub Encrypt(strDBNotEncrypted As String, strDBEncrypted As String)
Dim je As New JRO.JetEngine
je.CompactDatabase SourceConnection:="Data Source=" _
& strDBNotEncrypted & ";", _
DestConnection:="Data Source=" & strDBEncrypted & _
"; Jet OLEDB:Encrypt Database=True"
End Sub
This subroutine receives two parameters. The first is the name of the database you want to encrypt. The second is the name you want to assign to the encrypted database. The code issues the CompactDatabase method on the JetEngine object. This method receives two parameters: the name of the original database to encrypt and the name for the new encrypted database. Notice that the data source for the destination includes information indicating that you want to encrypt the database being compacted.