ACC: How to Recover a Table Deleted from a Database

Article translations Article translations
Article ID: 179161 - View products that this article applies to.
This article was previously published under Q179161
Moderate: Requires basic macro, coding, and interoperability skills.
Expand all | Collapse all


This article shows you how to create a sample Visual Basic for Applications function that you can use to recover a table deleted from a Microsoft Access for Windows 95 and Microsoft Access 97 database under the following conditions:
  • The database has not been closed since the deletion of the table.
  • The database has not been compacted since the deletion of the table.
  • The table was deleted using the Microsoft Access user interface.
NOTE: If multiple tables have inadvertently been deleted, this function recovers only the last table that was deleted. The other tables are lost.
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.


The following sample function recovers the last table deleted within a Microsoft 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 a table has recently been deleted, skip step 1, because if you have closed Microsoft Access or the database, the deleted table is not recoverable.
  1. Open your database in Microsoft Access.
  2. In the Database window, click the Modules tab, and then click New.
  3. Type or paste the following code in the module that you have just created:

    NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character indicates that the code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For additional information refer to the Microsoft Access 97 Readme File(Acread80.wri). This file is installed by default in the C:\Program Files\Microsoft Office\Office folder.
        Function undo()
          Dim db As Database, strTablename As String
          Dim i As Integer, StrSqlString As String
          Set db = CurrentDb()
          For i = 0 To db.TableDefs.Count - 1
          If Left(db.TableDefs(i).Name, 4) = "~tmp" Then
             strTablename = db.TableDefs(i).Name
             StrSqlString = "SELECT DISTINCTROW [" & strTablename & _
               "].* INTO MyUndeletedTable FROM [" & strTablename & "];"
             DoCmd.SetWarnings False
             DoCmd.RunSQL StrSqlString
             DoCmd.SetWarnings True
             MsgBox "A table has been restored as MyUndeletedTable", _
             GoTo Exit_undo
          End If
          Next i
          MsgBox "No Recoverable Tables Found", vbOKOnly, "Not Found"
             Set db = Nothing
             Exit Function
             MsgBox Err.Description
             Resume Exit_undo
        End Function
  4. On the Debug menu, click "Compile and Save All Modules" (or in Microsoft Access for Windows 95, on the Run menu click "Compile All Modules"; then on the File menu, click "Save All Modules").
  5. Save the Module as RecoverTable.

    To test this function, type the following line in the Debug window, and then press ENTER:


For more information about TableDefs, search the Help Index for "Tabledef Object."


Article ID: 179161 - Last Review: January 22, 2007 - Revision: 4.3
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
kbhowto kbprogramming KB179161
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from