joesmow
asked on
Problem deleting an access database
I am trying to close a database and delete the mdb file. However, when I do this, it does not delete the file because the file is still locked. I get a runtime error '70' Permission Denied. Using DAO 3.6, is there a way to check to see if the mdb is still locked and wait until it is ok to delete it or is there a better way to do this?
If Not (Dbs Is Nothing) Then
Dbs.Close
Set Dbs = Nothing
Data1.Database.Close
Data2.Database.Close
DoEvents
End If
sFileSpec = ProjectPath & "\" & ProjectName & ".mdb"
If Dir(sFileSpec) <> vbNullString Then Kill sFileSpec
If Not (Dbs Is Nothing) Then
Dbs.Close
Set Dbs = Nothing
Data1.Database.Close
Data2.Database.Close
DoEvents
End If
sFileSpec = ProjectPath & "\" & ProjectName & ".mdb"
If Dir(sFileSpec) <> vbNullString Then Kill sFileSpec
Use Dir$ function to locate yourdbname.ldb file, if it is still there, someone is using it.
Something like this:
if dir$("c:\dbpath\mydb.ldb", vbarchive) ="" then
kill "c:\dbpath\mydb.mdb"
end if
Something like this:
if dir$("c:\dbpath\mydb.ldb",
kill "c:\dbpath\mydb.mdb"
end if
Seeing your code, maybe you are having time problems between close the db (data control releases de connection) and try to kill it.
ASKER
First, what properties do I use to open it in Exclusive mode?
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb, ........)
Second, if I am openning it again, it seems like I will have the same problem when I close it again and try to delete it.
Richie,
Timing is the problem. It tries to kill it before the file is completely released. If I use a loop like the following then the file is never released:
While Dir("c:\mydb.ldb) <> ""
DoEvents
Wend
Or
While Not Dbs Is Nothing
DoEvents
Wend
Any other suggestions?
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb, ........)
Second, if I am openning it again, it seems like I will have the same problem when I close it again and try to delete it.
Richie,
Timing is the problem. It tries to kill it before the file is completely released. If I use a loop like the following then the file is never released:
While Dir("c:\mydb.ldb) <> ""
DoEvents
Wend
Or
While Not Dbs Is Nothing
DoEvents
Wend
Any other suggestions?
ASKER
First, what properties do I use to open it in Exclusive mode?
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb, ........)
Second, if I am openning it again, it seems like I will have the same problem when I close it again and try to delete it.
Richie,
Timing is the problem. It tries to kill it before the file is completely released. If I use a loop like the following then the file is never released:
While Dir("c:\mydb.ldb) <> ""
DoEvents
Wend
Or
While Not Dbs Is Nothing
DoEvents
Wend
Any other suggestions?
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb, ........)
Second, if I am openning it again, it seems like I will have the same problem when I close it again and try to delete it.
Richie,
Timing is the problem. It tries to kill it before the file is completely released. If I use a loop like the following then the file is never released:
While Dir("c:\mydb.ldb) <> ""
DoEvents
Wend
Or
While Not Dbs Is Nothing
DoEvents
Wend
Any other suggestions?
ASKER
Increasing points to 1000.
ASKER
Sorry. It will not let me go above 500.
ASKER
Sorry. It will not let me go above 500.
ASKER
Sorry. It will not let me go above 500.
ASKER
Sorry. It will not let me go above 500.
It's the third parameter in the OpenDatabase statement:
Set db = OpenDatabase("bob", adOpenDynaset, True)
Set db = OpenDatabase("bob", adOpenDynaset, True)
I'm sorry, I got that wrong. It's the second parameter, the third parameter is read-only. So, that would be:
Set db = OpenDatabase("bob", True, True)
Set db = OpenDatabase("bob", True, True)
First, what properties do I use to open it in Exclusive mode?
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb,True)
When you get error 70, it mean some other program has opened the database. So you don't realy need to check again. Just use error handling
On error goto handler
Exit sub
Handler:
Select case err.number
Case 70
'Database open, do whatever you need to do to close
Resume
Case else
End Select
End Sub
Set dbs = dbEngine.OpenDatabase ("C:\data.mdb,True)
When you get error 70, it mean some other program has opened the database. So you don't realy need to check again. Just use error handling
On error goto handler
Exit sub
Handler:
Select case err.number
Case 70
'Database open, do whatever you need to do to close
Resume
Case else
End Select
End Sub
ASKER
It still doesn't work. Even if I close the Exclusive database in the error handler, by the time it gets back to the kill statement, the database is still locked.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bingo! I went back through and found a recordset I forgot to close. When I closed the recordset properly, it allowed me to delete the file. Thanks everyone.
Glad to help.
Thanks for "A" grade.
Thanks for "A" grade.
I know it sounds like a kludge, but this is what MS recommends.