恢复从数据库中删除的表Recover tables deleted from a database
09/25/2018
本文内容
本主题演示如何创建一个示例 Visual Basic for Applications (VBA) 函数, 在以下情况下, 您可以使用它来恢复从 Access 数据库中删除的表:This topic shows how to create a sample Visual Basic for Applications (VBA) function that you can use to recover tables deleted from an 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 tables were deleted.
使用 Access 用户界面删除了表。The tables were deleted by using the Access user interface.
表不包含任何多值字段或附件字段。The table does not contain any multivalue or Attachment fields.
请将以下过程粘贴到标准模块中。Paste the following procedure into a standard module.
Sub RecoverDeletedTable()
On Error GoTo ExitHere
Dim db As DAO.Database
Dim strTableName As String
Dim strSQL As String
Dim intCount As Integer
Dim blnRestored As Boolean
Set db = CurrentDb()
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 Sub
ErrorHandler:
MsgBox Err.Description
Resume ExitHere
End Sub
支持和反馈Support and feedback
有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.