compact and repair

S

SirPoonga

How do I compact and repair to a copy? I read in other threads that
when you do a compact and repair it should have asked for an output DB
name. I am not getting htat, it just compacts and repairs.
 
M

Mark via AccessMonster.com

Have Access open, but don't have a database loaded. Then just select
Compact & Repair from the menu and it will prompt you for the file to
compact and the file to save it to.
 
M

Micah Chaney

-Open Access -- NOT A particular DATABSE just ACCESS.
-From the File Menu | Tools | Database Utilities | Compact and Repair.
-It will ask you what database you want to Compact and Repair, browse and
find it.
-It will ask you what you want to Compact and Repair into...select that
database, or create a new one.
 
S

SirPoonga

On a related note, in VBA code (say code from a button click) have the
database compact and repair itself? Yes, not advised since you might
lose data. But for another project compact and repair takes place when
there is no data in the DB. It's a conversion DB to take data from an
old mainframe system and format it to a new system. Company is in a
conversion process right now. So when this needs to happen I delete
the previous information, then I'd like to compact and repair, then
import new data. All with the click of one button. However I am
seeing that as not possible since when you compact and repair it closes
and reopens the DB. So just doing a delete query and compact and
repair as one step in code would suffice.
 
D

Dirk Goldgar

SirPoonga said:
On a related note, in VBA code (say code from a button click) have the
database compact and repair itself? Yes, not advised since you might
lose data. But for another project compact and repair takes place
when there is no data in the DB. It's a conversion DB to take data
from an old mainframe system and format it to a new system. Company
is in a conversion process right now. So when this needs to happen I
delete the previous information, then I'd like to compact and repair,
then import new data. All with the click of one button. However I am
seeing that as not possible since when you compact and repair it
closes and reopens the DB. So just doing a delete query and compact
and repair as one step in code would suffice.

This can actually be done, though it requires an external helper module
if you are using Access 97 or 95. If you have Access 2000 or later, you
can get the database to compact itself without the use of any external
routine, but if you are using Access 97, you need to pass control to an
external module of some sort to close, compact, and
reopen the database. See this link for how to do it either way:

http://www.mvps.org/access/general/gen0013.htm
 
S

SirPoonga

I haven't tried this yet. Will this code close and reopen the
database? Doesn't it kinda have to since it actually creates another
DB? What does it do with that temp DB? Is that a backup DB or is that
what ends up as the new DB it renames?

***** Code Start *****
Public Sub CompactDB()

CommandBars("Menu Bar"). _
Controls("Tools"). _
Controls("Database utilities"). _
Controls("Compact and repair database..."). _
accDoDefaultAction

End Sub
' ***** Code End *****
 
D

Dirk Goldgar

SirPoonga said:
I haven't tried this yet. Will this code close and reopen the
database?
Yes.

Doesn't it kinda have to since it actually creates another
DB?
Yes.

What does it do with that temp DB? Is that a backup DB or is
that what ends up as the new DB it renames?

***** Code Start *****
Public Sub CompactDB()

CommandBars("Menu Bar"). _
Controls("Tools"). _
Controls("Database utilities"). _
Controls("Compact and repair database..."). _
accDoDefaultAction

End Sub
' ***** Code End *****

I can't remember -- if I ever knew exactly -- whether the compacted
"temp" DB is copied over the original DB, or whether the original DB is
deleted and the "temp" DB is renamed to the original name, or what. If
I were writing it, I would -- after successfully compacting into a new
DB -- rename the original to a backup name, then rename the new DB to
the original's name, and only then, if all has gone well up to this
point, delete the renamed original. But I'm not sure what the Compact
and Repair process actually does.
 
K

Ken Snell [MVP]

I think the Compact and Repair process compacts the original database into a
new database, then deletes the original database and renames the new one to
the old one's name.

However, the code that I use for compacting a database renames the original
file to a new name, and compacts the renamed file into a new file that is
given the original file's name. That way, the original is already a backup
copy before the compacting begins.


Public Function
CompactBackendDatabaseFile_Custom(strPathFilename_OriginalBEDB As String, _
strPathFilename_TemporaryBEDB As String) As Integer
' Ken Snell 10 February 2005
' *** THIS FUNCTION IS USED TO COMPACT A DATABASE FILE. THE ORIGINAL FILE IS
COPIED
' *** INTO A TEMPORARY FILE, AND THEN THAT TEMPORARY FILE IS COMPACTED INTO
A FILE
' *** THAT IS GIVEN THE SAME NAME AS THE ORIGINAL FILE NAME. THE FUNCTION
RETURNS A
' *** VALUE OF -1 IF A LOCK FILE EXISTS FOR THE ORIGINAL FILE (NO COMPACTION
DONE); A
' *** VALUE OF 0 IF THE COMPACTION HAD NO ERRORS; A VALUE OF 1 IF THE
FUNCTION CANNOT
' *** FIND THE ORIGINAL FILE; A VALUE OF 2 IF AN ERROR OCCURRED DURING THE
COMPACTION.


Dim intLocation As Integer
Dim strTempBEDB As String, strTemp As String
Dim strDrive As String, strDateTime As String
Const strLockFileExtension As String = "ldb"


On Error Resume Next

strDateTime = Format(Now, "mmmddyyyyhhnnssAmPm")
strTempBEDB = strPathFilename_TemporaryBEDB
intLocation = InStrRev(strTempBEDB, "\")
strTempBEDB = Left(strTempBEDB, intLocation) & strDateTime & _
Mid(strTempBEDB, intLocation + 1)

If Dir(Left(strPathFilename_OriginalBEDB,
Len(strPathFilename_OriginalBEDB) - 3) & _
strLockFileExtension) = "" Then
On Error GoTo Err_Compact_1
Name strPathFilename_OriginalBEDB As strTempBEDB
DoEvents
On Error GoTo Err_Compact_2
DBEngine.CompactDatabase strTempBEDB, strPathFilename_OriginalBEDB
DoEvents
Do Until Dir(strPathFilename_OriginalBEDB) <> ""
Call WasteTime(25)
Loop
On Error Resume Next
' This next line should be commented out if you don't want to delete the
original file
' but want to keep it as a backup -- you can rename the file as desired
Kill strTempBEDB
CompactBackendDatabaseFile_Custom = 0
Else
CompactBackendDatabaseFile_Custom = -1
End If

Exit Function


Err_Compact_1:
On Error Resume Next
MsgBox "The original database file cannot be found at this location:" & _
vbCrLf & " " & strPathFilename_OriginalBEDB & vbCrLf & _
"The file cannot be compacted.", vbExclamation, "Cannot Find The File!"
CompactBackendDatabaseFile_Custom = 1
Exit Function

Err_Compact_2:
On Error Resume Next
Kill strPathFilename_OriginalBEDB
FileCopy strTempBEDB, strPathFilename_OriginalBEDB
MsgBox "An error occurred during the compacting operation of the file!" & _
vbCrLf & _
"The file cannot be compacted.", vbExclamation, "File Compaction Error!"
CompactBackendDatabaseFile_Custom = 2
Exit Function

End Function
 
K

Ken Snell [MVP]

Sorry, I missed that subroutine that I use in my databases.

' *****************************************
' ** Subroutine WasteTime **
' *****************************************

Public Sub WasteTime(Optional ByVal xlngLoopCounter As Long = 10)
Dim xlngLooping As Long
On Error Resume Next
For xlngLooping = 0 To xlngLoopCounter
DoEvents
Next xlngLooping
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top