Imports System
Imports System.IO
Public Class Form1
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
' Will C:db1.mdb do Compact/repair
If Compact_mdb ("C:db1.mdb") Then MessageBox.Show ("OK!")
End Sub
Private Function Compact_mdb (ByVal strfile as String) as Boolean
' Jet Access (MDB) connection string; Jet (joint Engine Technology)
Dim STRCN = "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}"
' or ' Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}; Jet oledb:engine type=5 "
' Path.gettempfilename method: Create a unique staging file with a 0-bit life on the disk,
' And then return the full path to the file.
Dim strtmpfile as String = Path.GetTempFileName.Replace (". tmp", ". mdb") ' Change TMP file name to MDB
' Create an array of objects to store the arguments (parameters), source, purpose
Dim Objpara as Object () = New Object () {String.Format (STRCN, strfile), String.Format (STRCN, Strtmpfile)}
' Activator member: A method that includes the local or remote establishment of an object type, or obtains a reference to an existing remote object.
' Activator.CreateInstance method (type): Creates an instance of the specified type using the constructor that best matches the specified parameter.
Dim Objjro as Object = Activator.CreateInstance (Type.gettypefromprogid ("JRO. JetEngine "))
' Type.gettypefromprogid method: Gets the type associated with the specified program identifier (ProgID);
' Returns NULL if an error occurs while loading the type.
' JRO. JetEngine for Microsoft Jet and Replication Objects x.x Library
' Type.InvokeMember method
' Type.InvokeMember (String, BindingFlags, Binder, Object, object[])
ObjJRO.GetType.InvokeMember ("CompactDatabase", Reflection.BindingFlags.InvokeMethod, _
Nothing, Objjro, Objpara)
' uses the specified binding condition constraint and conforms to the specified list of arguments to call the specified member.
' BindingFlags enumeration Type, InvokeMethod specifies the method to be called.
File.delete (strfile) ' File.delete method: Delete The MDB file before the compact
File.move (Strtmpfile, strfile) ' File.move method: Change the Compact MDB file to (back) the correct file name
' Marshal.ReleaseComObject method to release JRO COM object
Runtime.InteropServices.Marshal.ReleaseComObject (OBJJRO)
Objjro = Nothing
Return True
End Function
End Class