We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

The Access DB tools provide a way to compact and repair access database.We can achieve the same thing through code in VB.NET using the following steps.
 
1. Add a reference to "Microsoft Jet and Replication Objects 2.6 Library"
 
2. Check if the database is in use , display a warning.
[VB.NET CODE STARTS]
 
Dim strAccessDatabasePath As String ="YOUR ACCESS DB PATH"
Dim LockedDbFileInfo As New System.IO.FileInfo(strAccessDatabasePath.Replace(".mdb", ".ldb"))
        If LockedDbFileInfo.Exists Then
             '   Warn user that the db is in use.
        End If
 
[VB.NET CODE ENDS]
 
3. Create a temporary folder and copy the DB
 
[VB.NET CODE STARTS]
 
Rename(strAccessDatabasePath, TempFolder.FullName & "\" & "tmp1.mdb")
 
[VB.NET CODE ENDS]
 
4. Create an object of JRO JetEngine and call the compact method , to compact the tempdb and replace at the original location. After compacting delete the temp folder along with its contents.
 
[VB.NET CODE STARTS]
 
Dim JRO As New JRO.JetEngine
 
JRO.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= " & TempFolder.FullName & "\" & "tmp1.mdb", _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strAccessDatabasePath & ";Jet OLEDB:Engine Type=5")
 
TempFolder.Delete(True)
 
[VB.NET CODE ENDS]


Related Tags:

VB.NET, Compact and Repair Access Database

Author: Amrita Dash

top

VB.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login