How to recover tables deleted from a database in Access 2000, Access 2002, or Access 2003

Moderate: Requires basic macro, coding, and interoperability skills.


This article applies only to a Microsoft Access database (.mdb).


Summary

This article shows you how to create a sample Visual Basic for Applications function that you can use to recover tables deleted from a Microsoft Access database under the following conditions:

  • The database has not been closed since the tables were deleted.
  • The database has not been compacted since the the tables were deleted.
  • The tables were deleted using the Microsoft Access user interface.

More Information

The following sample function will try to recover all deleted tables in an Access database. To create the sample function, follow these steps.

Note These steps assume that you are creating the sample function for future use. If instead you are adding the code directly to a database in which tables have been deleted, skip step 1. If you closed Microsoft Access or the database where you deleted tables, you cannot recover the deleted tables by using this function.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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.

  1. Open your database in Microsoft Access.
  2. In the Database window, click Modules under Objects, and then click New.
  3. Type or paste the following code in the module that you have just created:
    Function RecoverDeletedTable()
    On Error GoTo ExitHere

    '*Declarations*
    Dim db As DAO.Database
    Dim strTableName As String
    Dim strSQL As String
    Dim intCount As Integer
    Dim blnRestored As Boolean

    '*Init*
    Set db = CurrentDb()

    '*Procedure*
    For intCount = 0 To db.TableDefs.Count - 1
    strTableName = db.TableDefs(intCount).Name
    If Left(strTableName, 4) = "~tmp" Then
    strSQL = "SELECT DISTINCTROW [" & strTableName & "].* INTO " & Mid(strTableName, 5) & " FROM [" & strTableName & "];"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    MsgBox "A deleted table has been restored, using the name '" & Mid(strTableName, 5) & "'", vbOKOnly, "Restored"
    blnRestored = True
    End If
    Next intCount

    If blnRestored = False Then
    MsgBox "No recoverable tables found", vbOKOnly
    End If

    '*EXIT/ERROR*
    ExitHere:
    DoCmd.SetWarnings True
    Set db = Nothing
    Exit Function

    ErrorHandler:
    MsgBox Err.Description
    Resume ExitHere

    End Function
  4. On the Debug menu, click Compile database name.
  5. Save the Module as RecoverTable. To test this function, first create two tables, add rows, and then delete these two tables.
  6. Type the following line in the Immediate window, and then press ENTER:
    RecoverDeletedTable

References

For more information about the TableDefs collection, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type TableDefs collection in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Propiedades

Id. de artículo: 209874 - Última revisión: 30 oct. 2008 - Revisión: 1

Comentarios