Opening a Microsoft Jet Database in Read Only Mode

If you'd like to open a database for read-only access you must set the Connection object's Mode property to the adModeRead constant (see Hands-On 10-3). ADO connections are opened by default for shared, updatable access. Eight mode constants are available to control the type of data access allowed.

Table 10-3: Intrinsic constants of the Connection object's Mode property

Constant Name

Value

Type of Permission

adModeUnknown

0

Permissions have not been set yet or cannot be determined. This is the default setting.

adModeRead

1

Read-only permissions

adModeWrite

2

Write-only permissions

adModeReadWrite

3

Read/write permissions

adModeShareDenyRead

4

Prevents others from opening the connection with read permissions

adModeShareDenyWrite

8

Prevents others from opening the connection with write permissions

adModeShareExclusive

12

Prevents others from opening the connection

adModeShareDenyNone

16

Prevents others from opening the connection with any permissions

Part II

©Hands-On 10-3: Opening a Database in Read-Only Mode

1. In the Database window, press Alt+F11 to switch to the Visual Basic Editor window.

2. In the Visual Basic Editor window, choose Insert | Module.

3. In the module's Code window, type the Open_ReadOnly procedure shown below.

4. Choose Run | Run Sub/UserForm to execute the procedure.

Sub Open_ReadOnly()

Dim conn As ADODB.Connection Dim strDb As String

On Error GoTo ErrorHandler strDb = CurrentProject.Path & "\Northwind.mdb"

Set conn = New ADODB.Connection

' Open for read-only access

With conn

.Provider = "Microsoft.Jet.OLEDB.4.0;" .Mode = adModeRead

.ConnectionString = "Data Source=" & strDb .Open End With

MsgBox "Database was opened for read-only access."

conn.Close

Set conn = Nothing

MsgBox "Database was closed."

Exit Sub

ErrorHandler:

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

0 0

Post a comment