Public Function vntRepDB()
'**********************************************
' vntRepDB accepts a database name and path, for example:
' "C:\Path\DB.MDB"
' vntRepDB repairs the specified database if it is not open.
'**********************************************
' Hide all the warnings
DoCmd.SetWarnings False
' Variables
Dim strDBPathName As String
strDBPathName = "G:\NewYorkBilling\Tina\Investigations\Clearstream\clearstream.mdb"
' Initiate error handling routine
On Error GoTo err_vntRepDB
' Repair the database using the RepairDatabase method
DBEngine.RepairDatabase strDBPathName
' If the database is repaired successfully, vntRepDB will return a successful response of "Pass"
vntRepDB = "Pass"
Exit Function
err_vntRepDB:
' Error routine handles any attempt to repair an open database and additional unexpected errors.
Select Case Err.Number
Case 3356
MsgBox Err.Description & vbLf & "Please close all instances of the database and try again."
Case Else
MsgBox Err.Description
End Select
' If an error occurred, the vntRepDB function returns user defined run-time error 65535.
vntRepDB = CVErr(65535)
End Function
Public Function vntComDB()
'**********************************************
' vntComDB accepts a database name and path, for example:
' "C:\Path\DB.MDB"
' vntComDB compacts the specified database if it is not open.
'**********************************************
' Hide all the warnings
DoCmd.SetWarnings False
' Variables
Dim strDBPathName As String, strTempName As String
strDBPathName = "G:\NewYorkBilling\Tina\Investigations\Clearstream\clearstream.mdb"
' Initiate error handling routine
On Error GoTo err_vntComDB
' Create a temporary database using a random number in the Windows Temp directory. The input database
' will be compacted into this temporary database. The Environ Function returns the setting for the
' specified operating system variable.
Randomize
strTempName = Environ("Temp") & "\TempDB" & Int((99 * Rnd) + 1) & ".MDB"
' Compact the databse into a temporary database.
DBEngine.CompactDatabase strDBPathName, strTempName
' If the database is compacted successfully, delete the original database and rename the temporary
' database to the original database's name.
Kill strDBPathName
Name strTempName As strDBPathName
' If the database is compacted successfully, vntComDB will return a successful response of "Pass".
vntComDB = "Pass"
Exit Function
err_vntComDB:
' Error routine handles any attempt to compact an open database and additional unexpected errors.
Select Case Err.Number
Case 3356
MsgBox Err.Description & vbLf & "Please close all instances of the database and try again."
Case Else
MsgBox Err.Description
End Select
' If an error occurred, the vntRepDB function returns user defined run-time error 65534.
vntComDB = CVErr(65534)
End Function
Public Function CompactRepair()
'**********************************************
' CompactRepair accepts a database name and path, for example:
' "C:\Path\DB.MDB"
' CompactRepair calls the repair and compact procedures vntRepDB and vntComDB
'**********************************************
' vntRepReturn and vntComReturn store the results of teh vntRepDB and vntComDB procedures.
Dim vntRepReturn As Variant, vntComReturn As Variant
' Hide all the warnings
DoCmd.SetWarnings False
' Delete the lock file if it exists.
If Dir("G:\NewYorkBilling\Tina\Investigations\Clearstream\clearstream.ldb") = "clearstream.ldb" Then
Kill "G:\NewYorkBilling\Tina\Investigations\Clearstream\clearstream.ldb"
End If
' Execute the vntRepDB procedure
vntRepReturn = vntRepDB()
' Test for "Pass" or user-defined error.
If IsError(vntRepReturn) Then
MsgBox "Repair Failed" & vbLf & "Please Notify Robert Johnson @ x4-1945"
Else
MsgBox "Repair Successful"
End If
' Execute the vntComDb procedure.
vntComReturn = vntComDB()
' Test for "Pass" or user-defined error
If IsError(vntComReturn) Then
MsgBox "Compact Failed" & vbLf & "Please Notify Robert Johnson @ x4-1945"
Else
MsgBox "Compact Successful"
End If
End Function