I've seen this one asked quite a bit. Contrary to popular belief, you don't need to use archaic DAO to compact an Access database . . .

I didn't document this code tremendously, I think it is pretty easy to follow.

VB Code:
  1. ' On a form
  2.  
  3. Option Explicit
  4.  
  5. Private Sub Command1_Click()
  6.     Dim sSource As String
  7.     Dim sDest As String
  8.  
  9.     sSource = App.Path & "\Northwind.MDB"
  10.     sDest = App.Path & "\CompactNwind.MDB"
  11.  
  12.     If CompactDB(sSource, sDest) Then
  13.         MsgBox "Compact complete"
  14.     End If
  15.    
  16. End Sub
  17.  
  18. ' In a module
  19.  
  20. Option Explicit
  21.  
  22. 'From MSDN . . .
  23. '-----------------------------------------------
  24. 'Jet OLEDB:Engine Type  Jet x.x Format MDB Files
  25. '---------------------  ------------------------
  26. '       1                     JET10
  27. '       2                     JET11
  28. '       3                     JET2X
  29. '       4                     JET3X
  30. '       5                     JET4X
  31. '-----------------------------------------------
  32.  
  33. Public Function CompactDB(ByVal sSource As String, ByVal sDest As String) As Boolean
  34.     'Requires references to:
  35.     ' Microsoft Jet and Replication Objects 2.1 Library (or higher)
  36.     ' Microsoft ActiveX Data Objects 2.5 Library (or higher)
  37.     Dim iEngineType As Integer
  38.     Dim jro As jro.JetEngine
  39.     Dim cn As ADODB.Connection
  40.  
  41.    On Error GoTo CompactDB_Error
  42.    
  43.     sSource = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  44.               "Data Source=" & sSource
  45.    
  46.     ' Find the engine type to use when compacting database
  47.     Set cn = New ADODB.Connection
  48.     With cn
  49.         .Open sSource
  50.        iEngineType = .Properties("Jet OLEDB:Engine Type")
  51.        .Close
  52.     End With
  53.     Set cn = Nothing
  54.    
  55.     sDest = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  56.                "Jet OLEDB:Engine Type=" & iEngineType & _
  57.                ";Data Source=" & sDest
  58.    
  59.     Set jro = New jro.JetEngine
  60.     jro.CompactDatabase sSource, sDest
  61.     CompactDB = True
  62.     Set jro = Nothing
  63.  
  64. On Error GoTo 0
  65. Exit Function
  66. CompactDB_Error:
  67.     CompactDB = False
  68.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CompactDB of Module Module1"
  69.     ' Clean up any "junk" left behind
  70. On Error Resume Next
  71.     Set cn = Nothing
  72.     Set jro = Nothing
  73. On Error GoTo 0
  74. End Function