Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem deleting an access database

Posted on 2002-06-21
17
208 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:joesmow
  • 9
  • 4
  • 3
  • +1
17 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 7099546
The best way to check to see if the db is in use is to close it, then try to reopen it in Exclusive mode.  If this throws a run-time error, then the db is still in use.  If not, then you can close and delete it.

I know it sounds like a kludge, but this is what MS recommends.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7099612
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7099613
Seeing your code, maybe you are having time problems between close the db (data control releases de connection)  and try to kill it.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:joesmow
ID: 7099630
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?

0
 

Author Comment

by:joesmow
ID: 7099650
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?

0
 

Author Comment

by:joesmow
ID: 7099660
Increasing points to 1000.
0
 

Author Comment

by:joesmow
ID: 7099661
Sorry. It will not let me go above 500.
0
 

Author Comment

by:joesmow
ID: 7099663
Sorry. It will not let me go above 500.
0
 

Author Comment

by:joesmow
ID: 7099707
Sorry. It will not let me go above 500.
0
 

Author Comment

by:joesmow
ID: 7099711
Sorry. It will not let me go above 500.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7099751
It's the third parameter in the OpenDatabase statement:

Set db = OpenDatabase("bob", adOpenDynaset, True)
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7099755
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)
0
 
LVL 9

Expert Comment

by:Dalin
ID: 7099767
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

0
 

Author Comment

by:joesmow
ID: 7099796
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.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 500 total points
ID: 7099933
Do you have any recordset still open that avoid you to close db and connections properly?
0
 

Author Comment

by:joesmow
ID: 7099997
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7101093
Glad to help.
Thanks for "A" grade.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question