ACC2000: How to Recover a Table Deleted from a Database
ID: Q209874
|
The information in this article applies to:
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 a table deleted from a Microsoft Access database under the following conditions:
- The database has not been closed since the table was deleted.
- The database has not been compacted since the the table was deleted.
- The table was deleted using the Microsoft Access user interface.
NOTE: If multiple tables were inadvertently deleted, this function recovers only the last table that was deleted. The other tables are lost.
MORE INFORMATION
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, you cannot recover the deleted table.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals 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 needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
NOTE: The sample code in this article uses Microsoft Data Access
Objects. For this code to run properly, you need to reference
the Microsoft DAO 3.6 Object Library.
- Open your database in Microsoft Access.
- In the Database window, click Modules under Objects, and then click New.
- Type or paste the following code in the module that you have just created:
Function Undo()
Dim db As DAO.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", _
vbOKOnly,"Restored"
GoTo Exit_Undo
End If
Next i
MsgBox "No Recoverable Tables Found", vbOKOnly, "Not Found"
Exit_Undo:
Set db = Nothing
Exit Function
Err_Undo:
MsgBox Err.Description
Resume Exit_Undo
End Function
- On the Debug menu, click Compile database name.
- Save the Module as RecoverTable.
To test this function, type the following line in the Immediate window, and then press ENTER:
Undo
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.
Additional query words:
Restore Delete Recover
Keywords : kbdta AccCon
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto