CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2001
    Posts
    12

    Is it possible to Compact an Access database using a Visual Basic Form?

    I need to compact my "Inventory.mdb" and I'd like to create an .exe file that
    will compact this database. Can I use VB directly to compact the database or do I still have to call another .mdb that will compact my "Inventory.mdb" database?


  2. #2
    Join Date
    Aug 2000
    Posts
    49

    It is possible to Compact an Access database using a Visual Basic Form!

    Add Reference of Microsoft DAO 3.6 Library to your Project. You will get an Object Named DBEngine.
    Use DBEngine.CompactDatabase Method to compact the database into another New Database File. Then Delete the Previous (Uncompacted) Database. rename the New Database File to the previous datbase name.

    The compactdatabase method will use some other parameters. I don't remember them now, but you can refere to the online help for more details.

    Waqas...


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Is it possible to Compact an Access database using a Visual Basic Form?

    You can compact using ADOX

    To take advantage of JRO's CompactDatabase method, which will also repair
    the database, set a reference the JRO library, Microsoft Jet And
    Replication Objects 2.1 Library. Next, declare and set a JetEngine object
    variable, like so

    Dim JRO As JRO.JetEngine
    Set JRO = New JRO.JetEngine

    JRO.CompactDatabase _
    "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=C:\myData1.mdb", _
    "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=C:\myDataNewName.mdb"_
    & ";Jet OLEDB:Engine Type=4"


    'Delete the original database
    Kill "c:\myData1.mdb"
    'Rename the file back to the original name
    Name "c:\myDataNewName.mdb" As "c:\myData1.mdb"

    Set JRO = Nothing

    This command follows similar syntax as DAO's CompactDatabase method-you
    must provide the old database name and the new database name. In the
    case of JRO, however, you must also provide a valid connection string,
    as well as indicate the type of Jet engine to compact the database as.
    It used 4 to create a Jet Version 3.x database suitable for Access 97
    users. If you work with Access 2000 use the value 5 to create a Jet
    Version 4.x database.

    '-----example with password--------
    Dim JRO As JRO.JetEngine
    Dim strSource As String
    Dim strTarget As String

    Set JRO = New JRO.JetEngine

    strSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    "C:\MyDB1.mdb;" & _
    "Jet OLEDBatabase Password=password"

    strTarget = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    "C:\MyDB2.mdb;" & _
    "Jet OLEDB:Engine Type=4;" & _
    "Jet OLEDBatabase Password=password"

    Call JRO.CompactDatabase(strSource, strTarget)



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured