Opening a Microsoft Jet Database in Read Write Mode

Hands-On 10-2 demonstrates how to use ADO to open a database for shared access (read/write). By default, the Connection object's Open method opens a database for shared access. The Open_ReadWrite procedure uses the Connection object's Mode property to explicitly specify the type of access to a database. The Mode property must be set prior to opening the connection because it is read-only once the connection is open. Connections can be opened read-only, write-only, or read/write. You can also specify whether other applications should be prevented from opening a connection. The value for the Mode property can be one of the constants/values specified in Table 10-3. The Mode property will work only if the provider you're using to open the connection supports it.

® Hands-On 10-2: Opening a Database in Read/Write Mode

1. Open the Acc2003_Chap10.mdb file from the book's downloadable files, or create a new Microsoft Office Access database from scratch.

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

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

Creating and Manipulating Databases with ADO

4. In the module's Code window, type the Open_ReadWrite procedure shown below.

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

Sub Open_ReadWrite()

Dim conn As ADODB.Connection Dim strDb As String

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

Set conn = New ADODB.Connection

With conn

.Provider = "Microsoft.Jet.OLEDB.4.0;" .Mode = adModeReadWrite .ConnectionString = "Data Source=" & strDb .Open End With

MsgBox "Connection was opened."

conn.Close

Set conn = Nothing

MsgBox "Connection was closed."

Exit Sub ErrorHandler:

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

0 0

Post a comment