|
我之前在某外文网站上摘录到的:
Undelete Tables and Queries
When you delete a table/query in Access, they don't actually get permanently deleted until Access is compacted using the Compact & Repair menu option.
Instead, Access flags the objects as 'deleted' - fortunately for us these flags can be reversed. When the items get flagged as 'deleted' Access also renames them to ~TMPCLP##### (#=Number) or similar, so unfortunately the names of the items are lost. In Jet 4 files (Access 2000+) table names are usually recovered (by using the Unicode NameMap translation property of the table).
The VBA code in this article shows how this can be achieved... (I've commented the code so you can follow it through if you want to see how its done).
Option Compare Database
Option Explicit
' VBA MODULE: Undelete tables and queries in Microsoft Access
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 18/04/2005
'
' REQUIREMENTS: VBA DAO Reference, Access 97/2000/2002(XP)/2003
'
' This module will allow you to undelete tables and queries
' after they have been deleted in Access/Jet.
'
' Please note that this will only work if you haven't run the
' 'Compact' or 'Compact And Repair' option from Access/DAO.
' If you have run the compact option, your tables/queries
' have been permananetly deleted.
'
' You may modify this code as you please,
' However you must leave the copyright notices in place.
' Thank you.
'
' USAGE: Just import this VBA module into your project
' and call FnUndeleteObjects()
'
' If any un-deletable objects are found, you will be prompted
' to choose names for the undeleted objects.
' Note: In Access 2000, table names are usually recovered too.
Public Function FnUndeleteObjects() As Boolean
'Module (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
'Written 18/04/2005
On Error GoTo ErrorHandler:
Dim strObjectName As String
Dim rsTables As DAO.Recordset
Dim dbsDatabase As DAO.Database
Dim tDef As DAO.TableDef
Dim qDef As DAO.QueryDef
Dim intNumDeletedItemsFound As Integer
Set dbsDatabase = CurrentDb
For Each tDef In dbsDatabase.TableDefs
'This is actually used as a 'Deleted Flag'
If tDef.Attributes And dbHiddenObject Then
strObjectName = FnGetDeletedTableNameByProp(tDef.Name)
strObjectName = InputBox("A deleted TABLE has been found." & _
vbCrLf & vbCrLf & _
"To undelete this object, enter a new name:", _
"Access Undelete Table", strObjectName)
If Len(strObjectName) > 0 Then
FnUndeleteTable CurrentDb, tDef.Name, strObjectName
End If
intNumDeletedItemsFound = intNumDeletedItemsFound + 1
End If
Next tDef
For Each qDef In dbsDatabase.QueryDefs
'Note 'Attributes' flag is not exposed for QueryDef objects,
'We could look up the flag by using MSysObjects but
'new queries don't get written to MSysObjects until
'Access is closed. Therefore we'll just check the
'start of the name is '~TMPCLP' ...
If InStr(1, qDef.Name, "~TMPCLP") = 1 Then
strObjectName = ""
strObjectName = InputBox("A deleted QUERY has been found." & _
vbCrLf & vbCrLf & _
"To undelete this object, enter a new name:", _
"Access Undelete Query", strObjectName)
If Len(strObjectName) > 0 Then
|
|