This repair and compact also backs up the Access MDB.
Function ValidateMdb(MDB As String) As Boolean
This function repairs, compacts, and backs up a database fileand deletes stray LDB (locking files) as it goes. The concept is to copy the file to a BAK version, then repairand compact the BAK back to the original. The renaming keeps it frombeing opened before you get a change to compact it, and it provides a backup since the client doesn’t do that. I call it Validate because NOBODY wants to wait for a file to bebacked up or compressed but everybody wants their data validated!
'Sample'Dim bSuccess As Boolean'bSuccess = ValidateMdb("c:my documentsdb2.mdb") Pat Tormey [email protected] dbe As New DBEngine 'Set Ref to MS DAODim sLdb As String 'Locking file's NameDim sBak As String 'BackUp file's NameDim rv As Boolean 'My standard for a return variableOn Error GoTo ProcErr rv = False If Len(Dir(MDB)) = 0 Then 'No File Found GoTo ProcExit End If sLdb = Left$(MDB, InStr(MDB, ".")) & "LDB" sBak = Left$(MDB, InStr(MDB, ".")) & "BAK" Kill sLdb 'delete it if you can If Dir$(sLdb) <> "" Then 'is it still here? Still in Use GoTo ProcExit End If 'Locked If Dir$(MDB) <> "" Then 'this is to be sure I don't delete the BAK Kill sBak if the MDB isn't around Name MDB As sBak 'Prevents somebody else from opening it End If 'Is there an MDB dbe.RepairDatabase sBak 'repair the backup dbe.CompactDatabase sBak, MDB 'compact back to original rv = TrueProcExit: ValidateMdb = rv Exit FunctionProcErr: If Err = 53 Then 'Probably LDB not found Resume Next End If 'Err rv = False Resume ProcExitEnd Function