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!

How do I compact my Access 97 database from code?

Compacting Databases

How do I compact my Access 97 database from code?

by  oharab  Posted    (Edited  )
The simple answer is "You can't".
You cannot compact the database you are in through code in Access 97. To work round this I created this VBscript.

Copy all the text into a text file and call it CompactDB.vbs or something.

'**Start**

Dim objEngine
Dim objDB
Dim strDb1, strPath
Dim FSO

'Path to database: In this case the same as the script location
strPath=left(wscript.scriptfullname,len(wscript.scriptfullname)-len(wscript.scriptname))

strDb1 = strpath & "Data.mdb"


Set FSO = CreateObject("Scripting.FileSystemObject")


do while FSO.FileExists(Left(strDb1, len(strDb1)-3) & "ldb")=True
'As long as an ldb file exists, someone is in the database, so just do nothing until it goes.
loop

if msgbox("About to perform some basic self maintenance." & chr(10) & "It is vital you do not exit windows until you receive the confirmation message." & chr(10) & "Press Ok to continue or Cancel to stop the process.",1,"Impact XP")=1 then
'let them know we are about to start

Set objEngine = WScript.CreateObject("DAO.DBEngine.35")

call CompactDB(FSO, objEngine, strDB1, "")

msgbox "File maintenance complete"
end if

Function CompactDB (objFSO, objEngine, strDb, pwd)
'compact db

strdbtemp = Left(strDb, len(strDb)-3) & "ldb"
If FSO.FileExists(strdbTemp)=True then 'if ldb file exists, db is still open.
msgbox "You have not exited the file. Please close and try again."
Exit Function
End if

if FSO.FileExists(strDb1) = False then
Exit Function
End If

strdbtemp = Left(strDb, len(strDb)-3) & "tmp"
if pwd="" then
objEngine.CompactDatabase strDb, strdbtemp
else
objEngine.CompactDatabase strDb, strdbtemp, , , ";pwd=" & pwd
end if
If Err <> 0 Then Exit Function
fso.deletefile strdb
fso.copyfile strdbtemp, strdb
fso.deletefile strdbtemp
End Function

'**End**

In your database you call the code using the fHandleFile function found here:

faq705-1971

It waits until you are out of the database and then compacts it for you.

Easy when you know how!

Ben
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top