by Roni Saar (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 5.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
Although ADO specification does not provide objects to compact Microsoft Access databases, this capability can be achieved by using the ADO extension: Microsoft Jet OLE DB Provider and Replication Objects (JRO). This capability was implemented for the first time in the JET OLE DB Provider version 4.0 (Msjetoledb40.dll) and JRO version 2.1 (Msjro.dll). These DLL files are available after the install of MDAC 2.1. You can download the latest version of MDAC from the following Web site:
Inputs
the file you want to compact
Side Effects
No Side Effects
Public Function CompactDatabase(strFileName As String) As Boolean
Dim objJro As jro.JetEngine
Dim objFileSystem As FileSystemObject
Dim strTmpFileName As String
On Error GoTo EXIT_PROC
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTmpFileName = objFileSystem.GetSpecialFolder(TemporaryFolder).Path & "\" & objFileSystem.GetFileName(strFileName)
Set objJro = New jro.JetEngine
objJro.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFileName, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strTmpFileName & ";Jet OLEDB:Engine Type=5"
objFileSystem.CopyFile strTmpFileName, strFileName
objFileSystem.DeleteFile strTmpFileName, True
CompactDatabase = True
EXIT_PROC:
Set objFileSystem = Nothing
Set objJro = Nothing
End Function