Hello,
My problem is related to the usage of DAO with MFC. I have two
applications running concurrently on the same CPU. They both need to
read the same Access (.mdb) file. At times, they need to read it
simultaneously. What I have found, however, is that reading the .mdb
file will create some kind of lock file (.ldb) which will prevent
other processes from reading it. This is temporary, of course, until
the database is closed, but while it is open and another process tries
to get at it, bad stuff happens. When this occurs, I see a message
box that tells me that the app "could not lock file". In order to
fully illustrate my problem, I'm going to run through some of the
evolution of this code...
void CMyClass::OpenDatabase()
{
CTable tbl; // derived from CDaoRecordset
try{
tbl.Open();
while(!tbl.IsEOF()){
<--- reads the data ----->
tbl.MoveNext();
}
tbl.Close();
}
catch(CDaoException* dao){
if(tbl.IsOpen())
tbl.Close();
dao->ReportError();
dao->Delete();
}
The code I am working on is littered with the above approach. ThisQuote:}
requires that the database have the read-only attribute turned off. I
have come up with another solution (after looking at many newsgroup
posts), which allows the database to have the read-only attribute and
seems to eliminate the .ldb file, but something about it does not seem
right...
void CMyClass::OpenDatabase()
{
CDaoDatabase db; // added this line
CMyTable tbl(&db); // derived from CDaoRecordset
try{
db.Open(tbl.GetDefaultDBName(), TRUE, TRUE); // added this line
tbl.Open();
while(!tbl.IsEOF()){
<--- reads the data ----->
tbl.MoveNext();
}
tbl.Close();
db.Close(); // added this line
}
catch(CDaoException* dao){
if(tbl.IsOpen())
tbl.Close();
if(db.IsOpen()) // added this line
db.Close(); // added this line
dao->ReportError();
dao->Delete();
}
This opens the database, sets the exclusive flag to TRUE, and theQuote:}
read-only flag to TRUE. This looks counterintuitive to me, but it
seems to work the best of everything I've tried. I would proceed to
make all the necessary changes in the code, but there are so many of
them that I want some assurance that this will work. Unless I'm
mistaken, this will eliminate the .ldb file from ever coming into
existence. However, will this allow two applications, simultaneously
making this call on the same machine, to work without showing a "could
not lock file" message box? Will it create new issues? If so, what
would be the best approach?
Thanks in advance, your help is appreciated.