Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compact the database from the code

Status
Not open for further replies.

sabavno

Programmer
Jul 25, 2002
381
CA
Hi

Is there a way to code the process of compacting the database?

Thanks
 
The tricky thing about CompactDatabase is that it can't be used on the currently open database, only on another database that's closed. So what you can do, I guess, is set up another database (db2) with nothing but a module programmed to compact your database (db1). Then, make a command in db1 to open db2, then quit. In db2, compact db1, then reopen it.

Very messy, I know. Anyone know a neater way to do it? :)

--Ryan
 
Can't you activate the "Compact Database" menu option? I'm pretty sure that selecting this menu option causes Access to close and compact the database and then reopen the database. I don't have Access on this PC so I can't try this out. I'll check back when I have access to Access(!) to see if you solved the problem.

Hope this helps

Daren
Must think of a witty signature
 
Tools, Options, General and select Compact on Close

Neil Berryman
IT Trainer
neil_berryman@btopenworld.com
 
Or have a look at the FAQs.
I've written one: faq705-1955 called "How do I compact my Access 97 database from code?"
It will work for Access 2k+ as well. Youjust need to change the reference to DAO35 to DAO36

HTH

Ben ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Thanks oharab,

I followed all your steps described in the FAQs,
but when I call the function

Dim strFile As String
strFile = "V:\Polina\BlackBerryDatabase\CompactDB.vbs"

Call fHandleFile(strFile)

I get a compile error "Argument not optional" pointing to the call fHandleFile

What is the appropriate way to call that function?

Thanks
 
Have I not fixed that yet!!!
Sorry, you need to add one of the constants:
WIN_NORMAL 'Open Normal
WIN_MAX 'Open Maximized
WIN_MIN 'Open Minimized

so you get fhandlefile(strfile,winnormal)

Will edit that now.

Cheers

Ben ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Ok, thanks, now it works
It opens my CompactDB.vbs file
But it does not run the code in the CompactDB.vbs?

What should I do to force that code in vbs file to run?

Please help me with this

Thanks

 
By the sounds of it you do not have Scriptinf installed on your pc. It is usually installed with Internet Explorer 5 I think, but your system admin may have crippled itf.
To work round it, you will need to use another database file so:

Create a blank file and call it CompactDB.mdb

Copy this code into a module -

Function CompactExternalDB()
Dim jEngine As Object
Set jEngine = CreateObject("DAO.DBEngine.36") 'change this to DAO.DBEngine.35 if you are using Access 97
'Create a jet engine to work from
fn = Dir(Left(Command(), Len(Command()) - 3) & "ldb")
Do Until fn = ""
fn = Dir(Left(Command(), Len(Command()) - 3) & "ldb")
'check for an ldb file for the database we are compacting.
'can't sompact until everyone is out of it
Loop
jEngine.CompactDatabase Command(), Left(Command(), Len(Command()) - 3) & "cmp"
'compact the database to a file with a cmp extension
Kill Command
'delete the old file
Name Left(Command(), Len(Command()) - 3) & "cmp" As Command()
'rename the compacted file
Set jEngine = Nothing
Application.Quit acQuitSaveNone
'close this file
End Function

Save the module and create a new macro. The only thing you want in the macro is a RunCode command and type CompactExternalDB() in the option.

Save the macro with the name AutoExec and close the database file.

In the mdb you want to compact add the following subroutine:

Sub CompactThisDB()
Shell SysCmd(acSysCmdAccessDir) & "msaccess.exe " & "[PathToCompact.mdb]\compactdb.mdb /cmd " & CurrentDb.name
End Sub


Now when you call the CompactThisDB subroutine another database will open, the current database will close and be compacted.

There is no error checking and it is a little ugly at the minute and will not work with password protected files yet, but I will have a go at tarting it up later.

HTH

Ben ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Thanks a lot!

One more question though...so now in order to compact the database I would always have to keep the CompactDB.mdb?

There is no other way to compact it having just one current database?

Thanks again

I appreciate your help

 
That's correct.
I am sure there is some reason why you can't compact the database whilst you are in it, but I can't think for the life of me what it is!

The good news is CompactDB.mdb is pretty reusable. One day I will get round to putting an enhanced version of it on my website.

Ben ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top