Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Re: How can I Compact and Repair Database using Macro or Visual Basic code?


Message #1 by "Ralph Jones" <kilquonk@y...> on Mon, 21 Oct 2002 17:22:19
True, you can't c&r a db while in it. Here's some vbscript code I got from another newsgroup. 
Include this in the script, and use CompactDb("<path and filename>"). I put it in task manager 
to c&r all my dbs daily. (Type it as a text file with the extension .vbs. You can then run it as if 
an executable.)

Sub CompactDb(strDb)
'From http://www.swynk.com/friends/lesandrini/CompactMDB02.asp

' *****************  BEGIN CODE HERE  ' *****************
'
Dim objScript
Dim objAccess
Dim strPathToMDB
Dim strMsg

' ///////////// NOTE:  User must edit variables in this section /////
'
'  The following line of code is the only variable that need be edited
'  You must provide a path to the Access MDB which will be compacted
'
strPathToMDB = strDb
'
' ////////////////////////////////////////////////////////////////

' Set a name and path for a temporary mdb file
strTempDB = "C:\tmp00001.mdb"

' Create Access 97 Application Object
'Set objAccess = CreateObject("Access.Application.8")

' For Access 2000, use Application.9
Set objAccess = CreateObject("Access.Application.9")

' Perform the DB Compact into the temp mdb file
' (If there is a problem, then the original mdb is preserved)
objAccess.DbEngine.CompactDatabase strPathToMDB ,strTempDB

If Err.Number > 0 Then
' There was an error.  Inform the user and halt execution
strMsg = "The following error was encountered while compacting database:"
strMsg = strMsg & vbCrLf & vbCrLf & Err.Description
Else
' Create File System Object to handle file manipulations
Set objScript= CreateObject("Scripting.FileSystemObject")
                           
' Back up the original file as Filename.mdbz.  In case of undetermined
' error, it can be recovered by simply removing the terminating "z".
objScript.CopyFile strPathToMDB , strPathToMDB & "z", True

' Copy the compacted mdb by into the original file name
objScript.CopyFile strTempDB, strPathToMDB, True

' We are finished with TempDB.  Kill it.
objScript.DeleteFile strTempDB
End If

' Always remember to clean up after yourself
Set objAccess = Nothing
Set objScript = Nothing
'
' ******************  END CODE HERE  ' ******************
End Sub
Message #2 by "Gerald, Rand" <RGerald@u...> on Mon, 21 Oct 2002 11:52:48 -0500
Ralph,

Actually, you CAN compact and repair a database while you are in it.  
It's
just that you must be the ONLY user ie: you must have exclusive access 
to
the database.

The ADH (Access Developer's Handbook) has code (one form and one 
module) for
showing who is logged into a database, and to lock the database for
exclusive access.  It is intended for this very type of operation.  The 
code
can be easily added to any Access 2000 database to provide these 
features.

Reference:
User Lists and Connection Control
Chapter 2, Pages 84-88
Access 2000 Developer's Handbook
Volume 2: Enterprise Edition
Litwin, Getz & Gilbert
Sybex 2000


Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: Ralph Jones [mailto:kilquonk@y...]
Sent: Monday, October 21, 2002 12:22
To: Access
Subject: [access] Re: How can I Compact and Repair Database using Macro 
or
Visual Basic code?

True, you can't c&r a db while in it. Here's some vbscript code I got 
from
another newsgroup.
Include this in the script, and use CompactDb("<path and filename>"). I 
put
it in task manager
to c&r all my dbs daily. (Type it as a text file with the extension 
.vbs.
You can then run it as if
an executable.)

Sub CompactDb(strDb)
'From http://www.swynk.com/friends/lesandrini/CompactMDB02.asp

' *****************  BEGIN CODE HERE  ' *****************
'
Dim objScript
Dim objAccess
Dim strPathToMDB
Dim strMsg

' ///////////// NOTE:  User must edit variables in this section /////
'
'  The following line of code is the only variable that need be edited
'  You must provide a path to the Access MDB which will be compacted
'
strPathToMDB =3D strDb
'
' ////////////////////////////////////////////////////////////////

' Set a name and path for a temporary mdb file
strTempDB =3D "C:\tmp00001.mdb"

' Create Access 97 Application Object
'Set objAccess =3D CreateObject("Access.Application.8")

' For Access 2000, use Application.9
Set objAccess =3D CreateObject("Access.Application.9")

' Perform the DB Compact into the temp mdb file
' (If there is a problem, then the original mdb is preserved)
objAccess.DbEngine.CompactDatabase strPathToMDB ,strTempDB

If Err.Number > 0 Then
' There was an error.  Inform the user and halt execution
strMsg =3D "The following error was encountered while compacting 
database:"
strMsg =3D strMsg & vbCrLf & vbCrLf & Err.Description
Else
' Create File System Object to handle file manipulations
Set objScript=3D CreateObject("Scripting.FileSystemObject")
                         
' Back up the original file as Filename.mdbz.  In case of undetermined
' error, it can be recovered by simply removing the terminating "z".
objScript.CopyFile strPathToMDB , strPathToMDB & "z", True

' Copy the compacted mdb by into the original file name
objScript.CopyFile strTempDB, strPathToMDB, True

' We are finished with TempDB.  Kill it.
objScript.DeleteFile strTempDB
End If

' Always remember to clean up after yourself
Set objAccess =3D Nothing
Set objScript =3D Nothing
'
' ******************  END CODE HERE  ' ******************
End Sub

  Return to Index