VB Compress/Repair Access database operation source code

Source: Internet
Author: User
Tags access database

Frequently, the database often has problems with operations when working in an Access database. And the database is infinitely larger. In order not to affect the use we need to compress/repair the database. The following code is used to compress and repair the Access database de code. You can use it directly.  Public Declare Function GetTempPath Lib "kernel32" Alias "Gettemppatha" (ByVal nbufferlength as Long, ByVal Lpbuffer as String) as Long

Public Const MAX_PATH = 260

Public Sub compactjetdatabase (location as String, Optional backuporiginal as Boolean = True)

On Error GoTo Compacterr

Dim Strbackupfile as String

Dim Strtempfile as String

' Check if the database file exists

If Len (Dir) Then

' Perform backup if backup is required

If backuporiginal = True Then

Strbackupfile = Gettemporarypath & "Backup.mdb"



If Len (Dir (strbackupfile)) then Kill Strbackupfile



FileCopy location, Strbackupfile

End If

' Create temporary file name

Strtempfile = Gettemporarypath & "Temp.mdb"



If Len (Dir (strtempfile)) then Kill Strtempfile



' Compress database files via DBEngine

Dbengine.compactdatabase location, Strtempfile

' Delete the original database file

Kill Location

' Copy has just compressed the temporary database file to its original location

FileCopy Strtempfile, location

' Delete temporary files

Kill Strtempfile

End If







Compacterr:



Exit Sub

End Sub

Public Function Gettemporarypath ()

Dim Strfolder as String

Dim Lngresult as Long

Strfolder = String (MAX_PATH, 0)

Lngresult = GetTempPath (MAX_PATH, Strfolder)

If Lngresult <> 0 <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.