Code.GeekInterview.com
 
Code Samples MS Access
 

Recover Tables Data from Corrupt MS Access Database


Code ResourceAuthor: Lokesh M  

Difficulty Level:

Published: 22nd Jun 2007   Read: 3939 times  

Filed in: MS Access
Add Comment


 

 

Sponsored Links


 

 

One time or the other, probably someone could have lost data due to corrupt MS Access Database.  However Not all corrupted Access database are irrecoverable.  In most of the cases you can recover almost all the data from the corrupt MS Access Database.  The Code below provides one possible alternative to recover data from certain (not all) types of corrupted MS Access Database.  

Though MS Access comes with built in Compact and Repair tool, Not all corrupted data can be fixed using it.

If you are getting the following message,

"Microsoft Access has detected corruption in this file.  To try to repair the corruption, first make a backup copy of the file. Then, on the Tools menu, point to Database Utilities and click Compact and Repair Database.   If you are currently trying to repair this corruption then you will need to recreate this file or restore it from a previous backup."

You can try the code provided here below to recover data from the corrupted MS Access Database.

Imp: Make a copy of the Corrupted Database
Try this code only on the backup (Copied) file.
Please take backup even if your database is corrupted.

1. Create a blank database = "C:Recover_db.mdb"
2. Close and exit.
3. Create another blank database
4. Navigate to Modules Tab
5. Click on New
6. Paste the code provided below
7. Go to Menu Tools->References
8. Scroll down and Select "Microsoft DAO 3.6 Object Library"
9. Click OK
10. In Module Window Click on Recover_db() Function
11. Check database file names, after proper setup, Hit F5
12. Tables and data from corrupt database is recovered to new database C:Recover_db.mdb, Close all database, Open C:Recover_db.mdb, You will find your data here...
Also note that I have used C:Corrupt_db.mdb as the corrupted database file name, you will have to change it to your file name.

Please Note : This is one of the method to recover data. However I will not assure you that you can recover all corrupt database from this method. The solution may work for some database and may NOT work for others. Since the cause of corruption are several, the recovery method varies. This is only one such method.

 


Sample Code
  1.  
  2.  Dim db As DAO.Database
  3.     Set db = DAO.OpenDatabase("C:Corrupt_db.mdb")
  4.     Dim xtable As TableDef
  5.     For Each xtable In db.TableDefs
  6.         If Mid(xtable.Name, 1, 4) <> "MSys" Then
  7.             rSQL = "SELECT * INTO [" & xtable.Name & "] IN " & _
  8.               "'C:Recover_db.mdb'" & _
  9.               " FROM [" & xtable.Name & "]"
  10.             db.Execute rSQL
  11.         End If
  12.     Next xtable
  13.    
  14.     MsgBox "Process Complete."
  15. End Function
  16.  
Copyright GeekInterview.com


Next Article: Generate Invoice Number Series


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

Fantastic, this has saved my day, thankyou.
Comment posted by: J pritchett on 2007-07-11T13:29:22

Popular Coders

# Coder NameHits
1. Lokesh M12075
2. Shivanna6704
3. Rajani5320

Active Coders

# Coder NameCodes
1. Shivanna2
2. Lokesh M2
3. Rajani1

Refined Tags