Results 1 to 7 of 7

Thread: Compact and Reapair Access db from Access?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Compact and Reapair Access db from Access?

    I have the following code that compacts and repairs an access table from vb.

    Public Sub MaintainDataFile(MDBfilename)
    'init
    Dim vJet As New JRO.JetEngine
    Dim vBak As String
    Dim vBackup As String
    Dim vSource As String

    'if bak exists, kill it
    vBak = Left(MDBfilename, InStr(MDBfilename, ".")) & "bak"
    If CBool(Len(Dir(vBak))) Then Kill vBak

    'compact/repair & backup
    vSource = "Data Source=" & MDBfilename
    vBackup = "Data Source=" & vBak

    vJet.CompactDatabase vSource, vBackup
    Kill MDBfilename
    Name vBak As MDBfilename
    End Sub

    When I try this code from within the Access program, I get the error message that says the database has been placed in a state by user 'Admin' that prevents it from being opened or locked.

    Is there a way to use code to compact and repair an access database from within that Access program?

    Thanks
    David Wilhelm

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Here's what I use :

    VB Code:
    1. '*******************************************************************************
    2. ' COMPACTJETDATABASE (SUB)
    3. '
    4. ' DESCRIPTION:
    5. ' COMPACTS DATABASE TO TEMP FOLDER AND
    6. ' COPIES BACK TO ORIGINAL DESTINATION
    7. '*******************************************************************************
    8.  
    9. Public Sub COMPACTJETDATABASE(LOCATION As String, _
    10.     Optional BACKUPORIGINAL As Boolean = True)
    11.    
    12.     On Error GoTo COMPACTERR
    13.    
    14.     Dim STRTEMPFILE As String
    15.    
    16.     ' CHECK TO ENSURE DATABASE EXISTS
    17.    
    18.     If Len(Dir(LOCATION)) Then
    19.        
    20.         ' CREATE TEMPORARY FILE NAME
    21.        
    22.         STRTEMPFILE = "c:\temp.mdb"
    23.        
    24.         If Len(Dir(STRTEMPFILE)) Then Kill STRTEMPFILE
    25.        
    26.         ' PERFORM THE COMPRESSION
    27.        
    28.         DBEngine.CompactDatabase LOCATION, STRTEMPFILE
    29.        
    30.         ' REMOVE THE ORIGINAL DATABASE
    31.        
    32.         Kill LOCATION
    33.        
    34.         ' COPY THE TEMP COMPRESSES DATABASE
    35.         ' BACK TO ORIGINAL LOCATION
    36.        
    37.         FileCopy STRTEMPFILE, LOCATION
    38.        
    39.         ' DELETE THE TEMP FILE
    40.        
    41.         Kill STRTEMPFILE
    42.        
    43.     Else
    44.        
    45.     End If
    46.    
    47.     Exit Sub
    48.    
    49. COMPACTERR:
    50.    
    51.     Exit Sub
    52.    
    53. End Sub

    Don't know if it has any affect with the admin thing or not, but you are welcome to test it...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Same error

    Thanks, but this is the same thing as I was doing. I got the same error.

    Anyone else have any ideas? I think the problem is I can't copy the database back to it's self because I have the program open.
    David Wilhelm

  4. #4
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    You're having this problem from within Access? You can't compact a database when it's open, so I don't think you can do it from a bas module within access because to run the code the database has to be open.

    If you are using VB, then you have to close the database before compacting or repairing and then open it again.

  5. #5
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Like cafeenman said, you can't use any of the code methods of compacting and repairing mdb's from within Access, since the mdb must be closed first. From within Access, you'll have to use the Access menu option

  6. #6
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I wonder if he could write a macro that can access the menu option so he can still do it programically. I'm thinking it might fail because there will be code running and the database might not close, but it may work.

  7. #7
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Originally posted by cafeenman
    I wonder if he could write a macro that can access the menu option so he can still do it programically. I'm thinking it might fail because there will be code running and the database might not close, but it may work.
    I had the same thoughts (both good and bad) but since the macro only has to access the menu option itself, it may work. Worth a try anyway.

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