Setting a Database Password

You can implement share-level security by setting a database password. When you set a database password, the password dialog box will appear when you open the database. Only users with a valid password can open the database.

To set a database password you cannot use ADOX objects. Instead, you have to use the objects from the Microsoft Jet and Replication Objects (JRO) Library. Use the CompactDatabase method of the JRO JetEngine object and specify the Password parameter.

The following procedure sets the sample database password to "welcome." Prior to running the procedure in Hands-On 17-14, set a reference to the JRO Objects Library as shown in Figure 17-23.

Figure 17-23: Before writing procedures that set or change the database password, you must set a reference to the Microsoft Jet and Replication Objects Library. To do this, choose Tools | References in the Visual Basic Editor window and select the required library in the list of Available References.

Figure 17-23: Before writing procedures that set or change the database password, you must set a reference to the Microsoft Jet and Replication Objects Library. To do this, choose Tools | References in the Visual Basic Editor window and select the required library in the list of Available References.

Creating and Manipulating Databases with ADO

©Hands-On 17-14: Setting a Database Password

While this hands-on uses the sample database that you prepared in Chapter 1 of this book, you can use any unsecured Access database for this password changing exercise.

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

Sub Change_DBPassword()

Dim jetEng As JRO.JetEngine Dim strCompactFrom As String Dim strCompactTo As String Dim strPath As String

On Error GoTo ErrHandle strPath = CurrentProject.Path & "\"

strCompactFrom = "Acc2003_Chap01.mdb" strCompactTo = "Acc2003_Chap01A.mdb"

Set jetEng = New JRO.JetEngine

' Compact the database specifying the new database password jetEng.CompactDatabase "Data Source=" & strPath & strCompactFrom & ";", _ "Data Source=" & strPath & strCompactTo & ";" & _ "Jet OLEDB:Database Password=welcome"

ExitHere:

Set jetEng = Nothing Exit Sub ErrHandle:

If Err.Number = -2147217897 Then Kill strPath & strCompactTo Resume

Else

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

2. After you run the procedure in Hands-On 17-14, open the Acc2003_ Chap01A.mdb file. You should be prompted for the password. Type welcome to log in.

0 0

Post a comment