You are currently offline, waiting for your internet to reconnect

How to Encrypt a Microsoft Access Database in Visual Basic

This article was previously published under Q104875
This article has been archived. It is offered "as is" and will no longer be updated.
Database encryption has nothing to do with security. However, you can usedatabase encryption to prevent someone from using a file or disk editor toread and write data in a Microsoft Access .MDB file. This article shows byexample how to encrypt a Microsoft Access database file in Microsoft VisualBasic version 3.0 for Windows.
Microsoft Access reads and writes all data a page at a time. Each page isalways 2K in size. Encryption is done at the page level, not at the datalevel. This means the encryption process has no knowledge of what is on thepage, only that there is 2K of data that needs to be encrypted and written.or read and decrypted.

Everything in a Microsoft Access .MDB database file is encrypted, includingtables, queries, forms, indexes, and so on. Microsoft Access uses the RSAcompany algorithm for database encryption.

The overhead involved in encrypting and decrypting causes is a performancedegradation of approximately 10-15% in encrypted databases. Encrypted filescannot be compressed using tools such as PKZip, Stacker, MS-DOS version 6DoubleSpace, and so on.

Encryption in Visual Basic

Use the CompactDatabase statement in Microsoft Visual Basic version 3.0 forWindows to encrypt a Microsoft Access database file. For more informationon the CompactDatabase statement, review pages 90-92 in the Visual Basicversion 3.0 "Language Reference" manual.

Step-by-Step Encryption Example

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
  2. From the Windows menu, choose Data Manager.
  3. In Data Manager, choose New Database from the File menu. Then select either Microsoft Access 1.0 or Microsoft Access 1.1.
  4. Enter the name TESTING.MDB for the Microsoft Access file name that you are about to create.
  5. Click the New button and enter Table1 for the table name.
  6. Click the Add button and enter First Name as the Field Name. Then select Text for the Field Type and enter 15 as the Field Size.
  7. Click the Add button for Indexes, and enter First Name Index as the Index Name. Then select Unique, Primary and click Done.
  8. Click the Open button, then the Add button. Next enter a name (Bob, for example) into the First Name field. Then click the Add button.
  9. Close the Data Manager and add a Command button to Form1.
  10. Add the following code to the Command1 Click event procedure:
        Sub Command1_Click ()       Const DB_ENCRYPT = 2       Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"       '** Enter the following two lines as one, single line:       CompactDatabase "C:\VB\TESTING.MDB", "C:\VB\NEWTEST.MDB",          DB_LANG_GENERAL, DB_ENCRYPT    End Sub						
  11. From the Run menu, choose Start (ALT, R, S) to run the program. Click the Command1 button to encrypt the TESTING.MDB database file. To check the new NEWTEST.MDB file, choose Data Manager from the Window menu in Visual Basic version 3.0 for Windows. In the Data Manager, choose Open Database from the File menu. Then select the NEWTEST.MDB file.

Article ID: 104875 - Last Review: 12/04/2015 09:44:54 - Revision: 1.1

Microsoft Visual Basic 3.0 Professional Edition

  • kbnosurvey kbarchive KB104875