Solved VBA Compact & Repair (1 Viewer)

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
Is there any way to crete a Compact & Repair button and a module or macro to execute?
The user will not have access to Ribbon and nav Pane, so that's why the need.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:09
Joined
Oct 29, 2018
Messages
21,474
What are you trying to compact, the FE or the BE?
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
Trying to raplicate the whole behavior of C&R buildt in function
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
The buildt in overwrites the current file, while creating a backup
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
A limitation may be possible here, i guess. Tryed and failed few times. I fond some indications on net that the process may be prohibited, others are saying that it is possible... nothing worked
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:09
Joined
Oct 29, 2018
Messages
21,474
A limitation may be possible here, i guess. Tryed and failed few times. I fond some indications on net that the process may be prohibited, others are saying that it is possible... nothing worked
I have a solution for both FE and BE, but only for MDB when it's not split.
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
Using Access 2019

Code:
Sub CompactAndRepairDB()
    Dim strDBName As String
    strDBName = CurrentDb.Name
    Application.CompactRepair strDBName, strDBName
    ' Delete the original database file
    Kill strDBName
    ' Rename the backup file to the original database name
    Name strDBName & "_backup" As strDBName
    MsgBox "Database has been compacted and repaired."
End Sub

This runs into 7847 runtime error

Code:
Public Function CompactAndRepairDB()
    Dim strDBName As String
    Dim strBackupPath As String
    
    ' Get the name of the current database
    strDBName = CurrentDb.Name
    
    ' Set the path for the backup file to the user's desktop
    strBackupPath = Environ("USERPROFILE") & "\Desktop\" & Format(Date, "yyyy-mm-dd") & " " & Format(Time, "hh-mm-ss") & " backup.accdb"
    
    ' Compact and repair the database
    Application.CompactRepair strDBName, strBackupPath
    
    ' Display a message when the process is complete
    MsgBox "Baza de date a fost compactata si reparata" & vbNewLine & vbNewLine & _
           "O copie de rezerva a fost plasata pe Desktop." & vbNewLine & vbNewLine & _
           "Puteti sterge copia dupa ce va asigurati ca baza de date este functionala.", vbInformation, ""

End Function

This pops the 7846
 

Josef P.

Well-known member
Local time
Today, 07:09
Joined
Feb 2, 2023
Messages
827
Is there any way to crete a Compact & Repair button and a module or macro to execute?
The user will not have access to Ribbon and nav Pane, so that's why the need.
Would a command line switch also help?
Code:
"C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" %1 /compact
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
1676577409619.png
 

isladogs

MVP / VIP
Local time
Today, 06:09
Joined
Jan 14, 2017
Messages
18,235
The ability to compact the current database using code was removed in A2010.
I believe this is because there is a risk of corruption...as can happen using the compact on close option

Either compact using the ribbon or do so externally from another database or using a command line switch
 

Romio_1968

Member
Local time
Today, 08:09
Joined
Jan 11, 2023
Messages
126
The ability to compact the current database using code was removed in A2010.
I believe this is because there is a risk of corruption...as can happen using the compact on close option

Either compact using the ribbon or do so externally from another database or using a command line switch
I'm affraid that this is the answer.
 

isladogs

MVP / VIP
Local time
Today, 06:09
Joined
Jan 14, 2017
Messages
18,235
Neither of the examples you supplied will work.
You can use VBA to copy the current database to another file then compact that.
However you cannot overwrite the current file with the compacted copy whilst the original file is open
 

Users who are viewing this thread

Top Bottom