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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.