Encrypting a Database

To achieve a higher level of security and protect your database from unauthorized access, you can encrypt it. Prior to encrypting, secure your database by setting user and group permissions on database objects. To encrypt a database you must be the owner or the creator of the database, or a member of the Admins group in the workgroup information file (System.mdw) that was in use when the database was created.

Use the CompactDatabase method of the Microsoft Jet and Replication Objects (JRO) JetEngine object to encrypt or decrypt a database. To use the JRO JetEngine object, you must set a reference to the Microsoft Jet and Replication Objects Library. To encrypt the database, set the Jet OLEDB:Encrypt Database property to True in the connection string destination argument of the CompactDatabase method.

After a database has been encrypted it cannot be read or written to directly by using any utility program or word processor. The procedure in Hands-On 17-16 creates an encrypted version of the Northwind sample database.

Part II

©Hands-On 17-16: Encrypting a Database

The procedure in this hands-on should be run after you have initially secured your database by creating the necessary user and group accounts and assigned user and group permissions on database objects.

1. Insert a new module and, in the module's Code window, enter the EncryptDb procedure as shown below.

Sub EncryptDb()

Dim jetEng As JRO.JetEngine Dim strCompactFrom As String Dim strCompactTo As String Dim strPath As String strPath = CurrentProject.Path & "\" strCompactFrom = "Northwind.mdb" strCompactTo = "Northwind_Enc.mdb"

On Error GoTo HandleErr

' Use the CompactDatabase method to create ' a new, encrypted version of the database Set jetEng = New JRO.JetEngine jetEng.CompactDatabase "Data Source=" & _ strPath & strCompactFrom & ";", _ "Data Source=" & strPath & strCompactTo & ";" & _ "Jet OLEDB:Encrypt Database=True"


Set jetEng = Nothing Exit Sub HandleErr:

MsgBox Err.Number & ": " & Err.Description Resume ExitHere End Sub

0 0

Post a comment