Compact Repair Database (Close Database)

Posted on 1999-11-03
Medium Priority
Last Modified: 2010-05-02
I have the code to compact and repair a database. But I receive the error 3356 the database is open.. What is the best way to close the database, if it is open. I have tried db.Close but it must be open somewhere else in the project.  Any suggestions?
Question by:TEALTEAL

Expert Comment

ID: 2181152
Declare your db and rs as public in a module

then close on exiting pgm

set db = nothing
set rs = nothing

Author Comment

ID: 2181515
Thanks, I've tried that but its still giving the error.

Expert Comment

ID: 2181773
I'd be hesitant about declaring recordsets as public. Public variables have their place, but I have had numerous problems with recordsets that are declared public. The data tends to change underneath you.

Try the following instead :

  Dim ws As Workspace
  Dim Db As Database
  Dim Rs As Recordset
  For Each ws In Workspaces
    For Each Db In ws.Databases
      For Each Rs In Db.Recordsets
        Set Rs = Nothing
      Set Db = Nothing
    Set ws = Nothing
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Expert Comment

ID: 2181774
Sorry, forgot to mention, declaring the Db object as public should be fine though.

Accepted Solution

ventond earned 200 total points
ID: 2183044
You have to make sure that ALL open databases closed, including any Data Controls, internal Databases, etc. Because my main app has so many of these things I have a seperate app to do just these things. By closing the main app I know all databases are closed. Troywillmot's comment will work assuming all "database opens" are on one form and are all defined using that workspace and that database.

Expert Comment

ID: 2184263
It would work across multiple forms/modules if an object was declared as type workspaces and set to dbengine.workspaces

That should go through any databases opened on workspaces in the workspaces collection for the current application.

It wouldn't do datacontrols etc. though.

It works regardless of what workspace, so long as the workspace if properly appended to the workspaces collection.
If not workspace is explcitly used when opening a database I believe workspaces(0) is used, which is automatically placed in the workspaces collection (I think).

It also works independently of the database, seeing as the database connections are attached to a workspace, and each workspace has its connections closed.

In short, it should work for all connections that are attached to workspaces in the workspaces collection.

We use this code in a module of an App with about 30/40 forms, 5 classes and 4 modules. It successfully closes ALL of our database connections... but we don't use data controls etc. and mostly we don't explicitly create workspaces, we just use workspaces(0). In those cases where we do create a workspaces, we always go Workspaces.Append Ws etc.

.... Nothing wrong with your answer though.

Expert Comment

ID: 5609244
I'd rather not make any of my databases public. I solved this problem in an application by placing the compact statement into the splash screen.
The splash screen fires before any other forms are loaded (ie., before any of the databases are opened)
There is a date checking routine to fire the compact process once per week.

If MonthView.DayOfWeek = mvwWednesday Then

Kill "c:\pos\posbak.mdb"
FileCopy "c:\pos\pos.mdb", "c:\pos\posbak.mdb"
CompactDatabase "C:\pos\pos.mdb", "c:\pos\pos1.mdb"

Kill "c:\pos\pos.mdb"
FileCopy "c:\pos\pos1.mdb", "c:\pos\pos.mdb"
Kill "c:\pos\pos1.mdb"
End If

I create a backup of my mdb file before doing the compact process (ya never know when you'll need a good backup file). It works. It's not clean, but it works

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

597 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