Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Access: how to compact current database in VBA

Tags:

Pretty simple question, I know.

like image 624
Nick Avatar asked Sep 22 '09 13:09

Nick


People also ask

What does compacting do in Access?

Microsoft Access provides a Compact and Repair Utility, which performs two separate functions. First, it compacts the database to eliminate wasted space and, secondly, it attempts to repair the database, if it was corrupted.


2 Answers

If you want to compact/repair an external mdb file (not the one you are working in just now):

Application.compactRepair sourecFile, destinationFile 

If you want to compact the database you are working with:

Application.SetOption "Auto compact", True 

In this last case, your app will be compacted when closing the file.

My opinion: writting a few lines of code in an extra MDB "compacter" file that you can call when you want to compact/repair an mdb file is very usefull: in most situations the file that needs to be compacted cannot be opened normally anymore, so you need to call the method from outside the file.

Otherwise, the autocompact shall by default be set to true in each main module of an Access app.

In case of a disaster, create a new mdb file and import all objects from the buggy file. You will usually find a faulty object (form, module, etc) that you will not be able to import.

like image 66
Philippe Grondier Avatar answered Oct 23 '22 04:10

Philippe Grondier


If you have the database with a front end and a back end. You can use the following code on the main form of your front end main navigation form:

Dim sDataFile As String, sDataFileTemp As String, sDataFileBackup As String Dim s1 As Long, s2 As Long  sDataFile = "C:\MyDataFile.mdb" sDataFileTemp = "C:\MyDataFileTemp.mdb" sDataFileBackup = "C:\MyDataFile Backup " & Format(Now, "YYYY-MM-DD HHMMSS") & ".mdb"  DoCmd.Hourglass True  'get file size before compact Open sDataFile For Binary As #1 s1 = LOF(1) Close #1  'backup data file FileCopy sDataFile, sDataFileBackup  'only proceed if data file exists If Dir(sDataFileBackup vbNormal) <> "" Then          'compact data file to temp file         On Error Resume Next         Kill sDataFileTemp         On Error GoTo 0         DBEngine.CompactDatabase sDataFile, sDataFileTemp          If Dir(sDataFileTemp, vbNormal) <> "" Then             'delete old data file data file             Kill sDataFile              'copy temp file to data file             FileCopy sDataFileTemp, sDataFile              'get file size after compact             Open sDataFile For Binary As #1             s2 = LOF(1)             Close #1              DoCmd.Hourglass False             MsgBox "Compact complete " & vbCrLf & vbCrLf _                 & "Size before: " & Round(s1 / 1024 / 1024, 2) & "Mb" & vbCrLf _                 & "Size after:    " & Round(s2 / 1024 / 1024, 2) & "Mb", vbInformation         Else             DoCmd.Hourglass False             MsgBox "ERROR: Unable to compact data file"         End If  Else         DoCmd.Hourglass False         MsgBox "ERROR: Unable to backup data file" End If  DoCmd.Hourglass False 
like image 42
user1467890 Avatar answered Oct 23 '22 03:10

user1467890