Me is use a Function when a trap a error in DAO.
If you have a corrupt database when you try to open in DAO the error #3343 (Access 97) is display and description of the name and the path of database corrupted. Finaly i extract the name and the path from description i pass in the Function to Repair and Compact DB.
Private Function MyFunction()
Dim bRetry as Boolean
On Error Goto MyFunction_Err
Set db= OpenDatabase("C:\Test\DBCorrupt.mdb"
etc...
db.close:set db = nothing
Exit Function
MyFunction_Err:
DisplayErrorMessage Err.Number, Err.Description, "MyFunction", bRetry
Err.Clear
if bRetry then Resume
set db = nothing
End Function
Public Sub DisplayErrorMessage(varErrorNum As Variant, strDescription As String, strSubFunctionName As String, bRetry As Boolean)
Const ERR_BD_CORRUPTION As Variant = 3343
If varErrorNum = ERR_BD_CORRUPTION Then
'Extract Path and Database Name from Description.
For iCounterX = 1 To Len(strDescription) - 5
If MID(strDescription, iCounterX, 5) = ".mdb'" Then
Exit For
End If
Next iCounterX
For iCounterY = iCounterX To 1 Step -1
If MID(strDescription, iCounterY, 1) = "'" Then
iCounterY = iCounterY + 1
Exit For
End If
Next iCounterY
strDB = MID(strDescription, iCounterY, iCounterX - iCounterY) & ".mdb"
If ToolsCompactRepairDB(strDB, False, True) Then
bRetry = True
End If
End If
End Function