?
Solved

How to Compact the Access MDB file through Coding ??

Posted on 2001-06-07
5
Medium Priority
?
405 Views
Last Modified: 2008-02-01
I want to how i can compact the database (Access MDB)through visual basic coding ?
0
Comment
Question by:krs_rama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 400 total points
ID: 6163508
Hi,

You can also use the CompactDatabase method of the Microsoft Jet and Replication Objects (JRO) 2.1 JetEngine

object to encrypt or decrypt a database. To use the JRO JetEngine object, you must set a reference to

the Microsoft Jet and Replication Objects 2.1 object library. When you use the CompactDatabase method,

you can't save the compacted (and optionally encrypted) database to the same name as the original database.

The CompactDatabase method takes two arguments to specify the source database and the destination database:

SourceConnection and DestConnection. Both the SourceConnection and DestConnection arguments take the

form of connection strings. Within the connection strings, you specify various connection properties

to determine how the source database is opened and how the destination database is compacted. At a minimum,

you must use the Data Source property in each connection string to specify the path and name of the

database. Additionally, to encrypt the database, you must include the Jet OLEDB:Encrypt Database property

in the connection string for the DestConnection argument. The following procedure uses these connection

properties to encrypt the database specified by the strSourceDB argument to the path and name specified

by the strDestDB argument:

Function EncryptDb(strSourceDB As String, _
                   strDestDB As String) As String
 Dim jetEngine          As JRO.JetEngine
 Dim strSourceConnect   As String
 Dim strDestConnect     As String

 ' Build connection strings for SourceConnection and
 ' DestConnection arguments.
 strSourceConnect = "Data Source=" & strSourceDB
 strDestConnect = "Data Source=" & strDestDB & ";" & _
                   "Jet OLEDB:Encrypt Database=True"

 Set jetEngine = New JRO.JetEngine

 ' Compact and encrypt the database specified by strSourceDB
 ' to the name and path specified by strDestDB.
 jetEngine.CompactDatabase strSourceConnect, strDestConnect

 Set jetEngine = Nothing
End Function

0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6163532
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6163538
if you are using dao you can do it directly with the dbengine from msdn:


    Dim dbsNorthwind As Database

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    ' Show the properties of the original database.
    With dbsNorthwind
        Debug.Print .Name & ", version " & .Version
        Debug.Print "  CollatingOrder = " & .CollatingOrder
        .Close
    End With

    ' Make sure there isn't already a file with the
    ' name of the compacted database.
    If Dir("NwindKorean.mdb") <> "" Then _
        Kill "NwindKorean.mdb"

    ' This statement creates a compact version of the
    ' Northwind database that uses a Korean language
    ' collating order.
    DBEngine.CompactDatabase "Northwind.mdb", _
        "NwindKorean.mdb", dbLangKorean

    Set dbsNorthwind = OpenDatabase("NwindKorean.mdb")

    ' Show the properties of the compacted database.
    With dbsNorthwind
        Debug.Print .Name & ", version " & .Version
        Debug.Print "  CollatingOrder = " & .CollatingOrder
        .Close
    End With

of course you want to leave off the korean collating order
here is an example that changes the db version


    Dim dbsNorthwind As Database
    Dim prpLoop As Property

    Set dbsNorthwind = OpenDatabase("Nwind11.mdb")

    ' Show the properties of the original database.
    With dbsNorthwind
        Debug.Print .Name & ", version " & .Version
        Debug.Print "  CollatingOrder = " & .CollatingOrder
        .Close
    End With

    ' Make sure there isn't already a file with the
    ' name of the compacted database.
    If Dir("Nwind20.mdb") <> "" Then _
        Kill "Nwind20.mdb"

    ' This statement creates a compact and encrypted
    ' Microsoft Jet 2.0 version of a Microsoft Jet version
    ' 1.1 database.
    DBEngine.CompactDatabase "Nwind11.mdb", _
        "Nwind20.mdb", , dbEncrypt + dbVersion20

    Set dbsNorthwind = OpenDatabase("Nwind20.mdb")

    ' Show the properties of the compacted database.
    With dbsNorthwind
        Debug.Print .Name & ", version " & .Version
        For Each prpLoop In .Properties
            On Error Resume Next
            If prpLoop <> "" Then Debug.Print "  " & _
                prpLoop.Name & " = " & prpLoop
            On Error GoTo 0
        Next prpLoop
        .Close
    End With


0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6163545
Here is a visual of VB5/Access 97:
http://www.mvps.org/access/modules/mdl0020.htm
0
 

Author Comment

by:krs_rama
ID: 6194051
I Accept this answer !!1
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question