Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Access 2002 Compact and Repair Method

Status
Not open for further replies.

tcalbaz

Technical User
Jun 12, 2001
13
0
0
US
I spent a bit of time trying to find out how to do this. Actually found this example in the Access 2002 Help File. Just wanted to share. [thumbsup2]

Ted
--------------

Compacts and repairs the specified database (.mdb) or Microsoft Access project (.adp) file. Returns a Boolean; True if the process was successful.

expression.CompactRepair(SourceFile, DestinationFile, LogFile)

expression Required. An expression that returns one of the objects in the Applies To list.

SourceFile Required String. The full path and filename of the database or project file to compact and repair.

DestinationFile Required String. The full path and filename for where the recovered file will be saved.

LogFile Optional Boolean. True if a log file is created in the destination directory to record any corruption detected in the source file. A log file is only created if corruption is detected in the source file. If LogFile is False or omitted, no log file is created, even if corruption is detected in the source file.

Remarks
The source file must not be the current database or be open by any other user, since calling this method will open the file exclusively.

Example
The following example compacts and repairs a database, creates a log if there's any corruption in the source file, and returns a Boolean value based on whether the recovery was successful. For the example to work, you must pass it the paths and file names of the source and destination files.

Function RepairDatabase(strSource As String, _
strDestination As String) As Boolean
' Input values: the paths and file names of
' the source and destination files.

' Trap for errors.
On Error GoTo error_handler

' Compact and repair the database. Use the return value of
' the CompactRepair method to determine if the file was
' successfully compacted.
RepairDatabase = _
Application.CompactRepair( _
LogFile:=True, _
SourceFile:=strSource, _
DestinationFile:=strDestination)

' Reset the error trap and exit the function.
On Error GoTo 0
Exit Function

' Return False if an error occurs.
error_handler:
RepairDatabase = False

End Function


 
Note:

When you compact a database using the Tools, Database Utilities, Compact and Repair function from the Menu bar, what basically happens is Access retrieves the name of the source database, then closes it.

With the Access application still running (not the database), it then opens the database exclusively in the background; performs the compact and repair, replaces the original with the compacted one, and reopens the database using the settings that were originally there.

The reason you can't do the same thing in code, to the database you are in, is because the running code is in the source database, and Access cannot close the database while code is running.
 
tcalbaz,
May I know how to compact and repair a back-end database? May be you can help. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top