tags:

views:

1763

answers:

5

I have a corrupt database. If I open it in MS Access, MS Access offers to repair it, and it succeeds.

How can I do that with code? On a machine where MS Access is not installed.

I know from trying it that JRO.JetEngine.CompactDatabase does NOT work.

In other words, I want to do what Access or JETCOMP.exe is doing, not what JRO.JetEngine.CompactDatabase is doing.

A: 

I'm not a MS Acccess guru, but it appears as though this utility contains the DLLs you'd need to compact and repair a corrupt database file.

Huuuze
I looked at the source code and it is doing what I'm doing, calling JRO.CompactDatabase.
Corey Trager
+1  A: 

Command-line switch for opening a .mdb file:

/compact

It repairs and compacts the database file.

If you leave out a target file name following the /compact switch, the file is compacted to the original name and folder. To compact to a different name, specify a target file.

micahwittman
Sorry, I clarified my question to indicate on a machine where MS Access is not installed.
Corey Trager
+1  A: 

Have you tried DBEngine.RepairDatabase [my.mdb]? (which doesn't seem to work any more even when you reference an earlier version)

However, if is happening so often that you need to code it, you probably have a bigger problem you should be solving first.

If you are willing to use separate utility, how about the Jetcomp.exe utility (http://support.microsoft.com/kb/295334 ). It is supposed to "be able to recover some databases that the Microsoft Access compact utility and the CompactDatabase method cannot." In which case, all you need to do is execute the external application.

e.g.,

Call Shell("Jetcomp.exe <arguments>")
CodeSlave
But see, http://support.microsoft.com/kb/294966:PRB: RepairDatabase Method Is No Longer Available in DAO 3.6
Corey Trager
But have you tried JetComp? It has nothing to do with the problem in that article, as it's a standalone executable.
David-W-Fenton
BTW, Repair as a separate operation was removed from Access with Access97 SR2, because people were repairing undamage MDBs and it was corrupting them. Since then, every COMPACT operation first checks if the MDB needs to be repaired, and repairsi t if it needs to. Thus, to repair, execute a compact.
David-W-Fenton
JetComp is fine, but I don't want to ship JetComp. I want to build its functionality into my app. That's what I meant by "programmatically".
Corey Trager
You can't do it. Period. Unless you can figure out how to reverse engineer the Jet DLLs. Good luck with that -- I certainly wouldn't recommend it. I don't know why you don't want to include the one tool MS provides for this purpose.
David-W-Fenton
Then I don't think it's doable. And it's probably not worth your effort to find a way - really this should be a rare event. Add a FAQ on how to fix a corrupt database.Or one better, fix the problem at is source. Switch to a more-reliable free database (MySQL, the runtime MS SQL, etc.)
CodeSlave
+1  A: 

You need to go to http://support.microsoft.com and search for the JetComp.exe utility, which will attempt to repair and compact your MDB without opening the file. The reason none of the suggestions above work is because they have to open the MDB to do their work, whereas JetComp doesn't open the file, but operates on it structurally.

If it can't recover your file (which does happen), then you'll have to go to a data recovery service. I recommend Peter Miller of PKSolutions.com.

--
David W. Fenton
David Fenton Associates

David-W-Fenton
Not the answer because I want to do it "programmatically".
Corey Trager
I'm afraid you're out of luck, then.
David-W-Fenton
BTW, what's wrong with the architecture of your app that you know your users will need extraordinary tools to repair corrupted MDBs? There is no reason why you should expect MDBs to corrupt (it's the exception rather than the rule in a well-architected app). Maybe you shouldn't be using Jet,
David-W-Fenton
A: 

Why not try Advanced Access Repair at http://www.datanumen.com/aar/ ? It recovers several important Access MDB files for me and is really helpful!

wangdong

wangdong
Does that tool work programmatically, other than via commandline? And are you personally associated with the product? The fact that you've just joined SO and posted two answers to questions about corruption that recommending the same tool suggests you're shilling for your employer or your own product. If you have an interest in this product, you should declare that.
David-W-Fenton