Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 23rd, 2006, 04:34 PM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compacting database

Is there a way to compact a database from another database but keep the original name?
I have found code to compact but you have to create a new database.

Thanks,
Scott
__________________
ScottP
  #2 (permalink)  
Old March 23rd, 2006, 07:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Scott,

You can do it with a little file juggling:

Sub Test()

   ' Running this line of code leaves you with your
   ' original database and a compacted temporary version
   ' of your original database on drive C:
   DBEngine.CompactDatabase "C:\Test.mdb", _
      "C:\TestTemp.mdb"

   ' Delete old backup file if there is one.
   If Dir("C:\Test.bak") <> vbNullString Then
      Kill "C:\Test.bak"
   End If

   ' Now change the extension of your original database to
   ' save as a backup file, and rename the compacted temporary
   ' version of your original database to the name of your original
   ' database.
   Name "C:\Test.mdb" As "C:\Test.bak"
   Name "C:\TestTemp.mdb" As "C:\Test.mdb"

End Sub

HTH,

Bob

  #3 (permalink)  
Old March 24th, 2006, 07:08 PM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Bob, it was the renaming thing I could not find anywhere. Works fine.

Thanks again,
Scott
  #4 (permalink)  
Old August 8th, 2006, 09:16 AM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was under the impression that only closed databases could be compacted. If I put that code word-for-word into a module and run it, it will not work. Obviously I'm missing something. Do I need to close the current database file (Test.mdb here) and open another one with the startup module containing this code and then close it automatically, only to open test.mdb back up again? I hope there is an easier way.

  #5 (permalink)  
Old August 8th, 2006, 10:40 AM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff, my question was specifically for compacting from another database so I believe this only works with the database to be compacted closed. I may be missing something but if you have the database open why don't you just compact it manually?

ScottP
  #6 (permalink)  
Old August 9th, 2006, 09:33 AM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it to work. Thanks.



Similar Threads
Thread Thread Starter Forum Replies Last Post
compacting database pblm MathLearner VB Databases Basics 8 September 27th, 2007 07:34 AM
Compacting Access Database problem imaley SQL Language 1 April 23rd, 2004 11:28 AM
Compacting Access Database from code imaley Access 6 March 31st, 2004 05:33 PM
Automatically Compacting a Database nanohurtz Access 2 March 22nd, 2004 10:25 AM
Compacting a large database takabyte Classic ASP Databases 1 February 5th, 2004 12:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.