Microsoft KB Archive/191737

= PRB: DAO MDB on Read-Only Media Must Be Opened Exclusively =

Article ID: 191737

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition
 * Microsoft Visual Studio 97 Service Pack 3
 * Microsoft Visual Studio 6.0 Enterprise Edition

-



This article was previously published under Q191737



SYMPTOMS
DAO applications that use a Microsoft Access .mdb file on a read-only media (CD-ROM, floppy disk write protected, a hard disk that is read only to the user, etc) must open the database file with exclusive access.

If you don't open the database exclusively, you get one of the following errors when attempting to open a recordset:


 * The Microsoft Jet Database engine cannot open the file 'YourMDBfile.mdb'. It is already opened exclusively by another user, or you need permission to view its data.
 * Couldn't Lock File



CAUSE
Jet requires the creation of a locking file (.ldb file) in order to open a database file in shared access mode. If you request shared access mode, Jet attempts to create the locking file, which fails on read-only media. If you open a database using exclusive mode, Jet does not need to create the locking file.



RESOLUTION
Open the database with exclusive access. One way to do this is to insert the following three lines into the wizard generated RecordView OnInitialUpdate as follows: static CDaoDatabase mdb;         //** Add db class here or in .h file. mdb.Open("D:\\RO.mdb",TRUE,TRUE); //** RO && Exclusive m_pSet->m_pDatabase= &mdb;       //** Assign DB to RecordSet class. Even though you must open the .mdb file with exclusive access, other applications can also open it read-only and exclusive.



MORE INFORMATION
The following code shows the three lines inserted into the RecordView OnInitialUpdate method: void CCDRomView::OnInitialUpdate{ m_pSet = &GetDocument->m_rOSet;   // Original wizard code. #ifdef FixCode //** Add db class here or in .h file. static CDaoDatabase mdb; //** Open it ReadOnly and Exclusive. mdb.Open("D:\\RO.mdb",TRUE,TRUE); //** Assign DB to RecordSet class. m_pSet->m_pDatabase= &mdb; #endif CDaoRecordView::OnInitialUpdate;  // original wizard code }

Steps to Reproduce Behavior

 * 1) Create a small .mdb file with one table and call it "RO.mdb".
 * 2) Use the MFC AppWizard to create a CDaoDatabase project using the table in the RO.mdb file.
 * 3) Build and test the application.
 * 4) Copy RO.mdb to a floppy disk, and set the write protect tab on the floppy.
 * 5) In the recordset class, change the GetDefaultDBName to point to the RO.mdb database file on the floppy disk.
 * 6) Rebuild and run the application. You get the error message.

Keywords: kbdatabase kberrmsg kbprb KB191737

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.