[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 137
  • Last Modified:

Compact Access Problem

I have this piece of code

Public Sub CompactDatabase()
    Dim JetEngine As JRO.JetEngine
    Dim strSourceConnect As String
    Dim strDestConnect As String
    Dim gbl_strDBFile As String
    Dim dbArchive As String
    Dim objFso As FileSystemObject
    Set JetEngine = New JRO.JetEngine

    gbl_strDBFile = "C:\db_ee.mdb"    
    dbArchive = Left(gbl_strDBFile, InStrRev(gbl_strDBFile, "\", Len(gbl_strDBFile))) & Split(Right(gbl_strDBFile, Len(gbl_strDBFile) - InStrRev(gbl_strDBFile, "\", Len(gbl_strDBFile))), ".")(0) & "1" & ".mdb"
    MsgBox dbArchive
    strSourceConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                       "Data Source=" & gbl_strDBFile & ";" & _
                       "User Id=sa;" & _
    strDestConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                     "Data Source=" & dbArchive & ";" & _
                     "User Id=sa;" & _
    JetEngine.CompactDatabase strSourceConnect, strDestConnect
    Set objFso = New FileSystemObject
    objFso.CopyFile dbArchive, gbl_strDBFile
    objFso.DeleteFile dbArchive 'delete the copy
    Set objFso = Nothing
    Set JetEngine = Nothing
End Sub

I got this error at this line
    JetEngine.CompactDatabase strSourceConnect, strDestConnect

Error is
"Cannot start your application. The workgroup information file is missing or opened exclusively by another user."

Note that the original file, gbl_strDBFile, has got a password and that is mypwd.
  • 2
1 Solution
try changing yuor connection strings to:

   strSourceConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                      "Data Source=" & gbl_strDBFile & ";Jet OLEDB:Database Password=mypwd"
   strDestConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & dbArchive & ";Jet OLEDB:Database Password=mypwd;Jet OLEDB:Engine Type=5"

I wouldn't add the overhead of FSO just to copy and delete a file. I would use:

FileCopy dbArchive, gbl_strDBFile
Kill dbArchive

Why don't you change your code slightly to below.

   JetEngine.CompactDatabase strSourceConnect, strDestConnect
   ' Compact successful
   If (DIR(strDestConnect) <> "") then
       Kill strSourceConnec 'delete souce
       Name strDestConnect AS strSourceConnec
   End if
End Sub

Make sure that in your code, before you compact the database you close all connections to the database and after compacting the database you reopen these connections.

Good Luck!

Try adding this to your connectionstrings:
Jet OLEDB:System database=<full_path_to_system.mdw>

Usually system.mdw exists in x:\Program Files\Microsoft Office\Office (office 2000) or
in your <WINSYSDIR>.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now