Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


758


LISTING 21.2
Using ADO to Open a Database Exclusively

Public Sub OpenDatabaseTest_ADO()
Dim cnn As ADODB.Connection
Dim str As String
str = “Provider=Microsoft.Jet.OLEDB.4.0;” _
& “Data Source=C:\Data\MyDB.accdb”
Set cnn = New ADODB.Connection
cnn.Mode = adModeShareExclusive
cnn.ConnectionString = str
cnn.Open
‘... Your code goes here ...
cnn.Close
Set cnn = Nothing
End Sub

Public Sub OpenDatabaseTest_DAO()
Dim db As DAO.Database
Set db = DBEngine.OpenDatabase(“C:\Data\MyDB.accdb”, False)
‘... Your code goes here ...
db.Close
Set db = Nothing
End Sub

When opening a database with DAO code, you pass a parameter to the OpenDatabase method
of the Workspace object to instruct the database engine which open mode to use. Notice that the
False value is passed as the second argument to the OpenDatabase method in Listing 21.2.

The syntax of the DAO OpenDatabase method is

Set Database = Workspace.OpenDatabase (dbName _
[, Exclusive] [, Read-only] [, Connect])

where:

l dbName is the name of the database to open

l (^) Exclusive instructs Access whether to open dbName in exclusive or Shared mode.
True means open the database exclusively; False means open in Shared mode.
l (^) Read-only is a instruction telling Access to open the database in read-only mode.
l Connect is the connect string required by the ODBC database.
By default, a database opened with the OpenDatabase method is opened in Shared mode
(Exclusive is False). You should, of course, specify either True or False as the Exclusive
value.

Free download pdf