Note The sample code in this article uses Microsoft Data Access
Objects. For this code to run properly, you must reference the Microsoft DAO
3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
To recover the data from the corrupted table in
Access, follow these steps:
- Make a copy of the corrupted database. Exclude the
corrupted tables. Name the database
Recovered_Database.
- Open the Access database that contains the corrupted
tables.
- Click Modules and then click
New.
- Put the following code in the new module:
Note The following code creates a table in Recovered_Database with the same name as the corrupted table
(<Corrupted_Table>).
Sub Recovery()
Dim db As Database
Dim sql As String
' Set the database to current database.
Set db = CurrentDb()
' Set sql to a string that represents the query to recover data from
' the corrupted table. <Corrupted_Table> is the name of corrupted table.
sql = "SELECT <Corrupted_Table>.* INTO <Corrupted_Table> in " & _
"'<Absolute_Path_of_Recovered_Database>\Recovered_Database.mdb'" & _
" FROM <Corrupted_Table>"
db.Execute sql
End Sub
- On the File menu, click
Save. Save the module as
Module1.
- On the Run menu, click Run
Sub/UserForm.
- Repeat step 3 to step 6 for each corrupted
table.
After you complete these steps:
- Test the recovered database before you return the database
to the production environment.
- Do not delete the damaged database until you confirm the
recovery.