Restore the method for deleting a table from the Access Database
Note: The sample code in this article uses Microsoft Data to access objects. To run the code properly, you must reference the Microsoft DAO 3.6 Object Library. Yes. Click the Visual Basic Editor in the tool menu to reference and ensure that the check box of the Microsoft DAO 3.6 Object Library is selected.
1. Open the database in Microsoft Access.
2. In the database window, click object, module, and then click New.
3. type or paste the following code. You can only create a module:
Copy the Code as follows:
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 it as the RecoverTable module. To test this function, create two tables, Add rows, and delete the two tables.
6. In the real-time window, type the following line and press ENTER:
RecoverDeletedTable
Note: For more exciting tutorials, please follow the help houseDesign tutorialTopic,