ACC1x: Function to Delete Table Without Using DoMenuItemID: Q94042
|
You can create and use the DeleteTable() function to delete a table without using the DoMenuItem() or SendKeys() functions.
Microsoft Access stores all references to objects (tables, queries, forms,
reports, macros, and modules) in a system table called MSysObjects. You can
delete an object from the MSysObjects table if you have permission to do
so.
To view the system objects in your database,
Option Explicit
Function DeleteTable (TableName as String)
Dim SQL As String, Msg As String
If DCount("[Name]", "MSysObjects", "[Name] = '" _
& TableName & "'") = 0 Then
DeleteTable = FALSE
Exit Function
End If
On Error GoTo DeleteErrorProc
' Suppress the "Do you want to delete these records?" message.
DoCmd SetWarnings False
' SQL statement to delete appropriate entries from the MSysColumns
' table follows:
SQL = "DELETE DISTINCTROW MSysObjects.Name, MSysColumns.*"
SQL = SQL & " FROM MsysObjects, MSysColumns,"
SQL = SQL & " MSysColumns INNER JOIN MSysObjects ON"
SQL = SQL & " MSysColumns.ObjectId = MSysObjects.Id"
SQL = SQL & " WHERE ((MSysObjects.Name = '" & TableName & "'));"
DoCmd RunSQL SQL
' SQL Statement to delete appropriate entries from MSysACEs table
' follows:
SQL = "DELETE DISTINCTROW MSysObjects.Name, MSysACEs.*"
SQL = SQL & " FROM MSysObjects, MSysACEs,"
SQL = SQL & " MSysObjects INNER JOIN MSysACEs ON"
SQL = SQL & " MSysObjects.Id = MSysACEs.ObjectID"
SQL = SQL & " WHERE ((MSysObjects.Name= '" & TableName & "'));"
DoCmd RunSQL SQL
' SQL statement to delete references to indexes in MSysIndexes follows:
SQL = "DELETE DISTINCTROW MSysObjects.Name, MSysIndexes.*"
SQL = SQL & " FROM MSysObjects, MSysIndexes,"
SQL = SQL & " MSysObjects INNER JOIN MSysIndexes ON"
SQL = SQL & " MSysObjects.Id = MSysIndexes.ObjectId"
SQL = SQL & " WHERE ((MSysObjects.Name= '" & TableName & "'));"
DoCmd RunSQL SQL
' SQL statement to delete references to table object from MSysObjects
' follows:
SQL = "DELETE DISTINCTROW MSysObjects.Name "
SQL = SQL & " FROM MSysObjects"
SQL = SQL & " WHERE ((MSysObjects.Name = '" & TableName & "'));"
DoCmd RunSQL SQL
' If you decide to update the Database window, include the following
' command:
SendKeys "{F11} %VQ%VT"'
' Test to make sure the object was deleted, then exit.
If DCount("[Name], "MSysObjects", "[Name] = '" _
& TableName & "'") = 0 Then
DeleteTable = True
Exit Function
EndIf
' If error occurs, display the error message and terminate with error.
DeleteErrorProc:
Msg = "An unexpected error has occurred." & Chr(13) & Chr(10)
Msg = Msg & "Error: " & Error$
MsgBox Msg
DeleteTable = False
Exit Function
End Function
Microsoft Access "User's Guide," version 1.0, chapter 25
Additional query words: functions sysfiles
Keywords : kbprg PgmObj
Version : 1.0 1.1
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: March 13, 1999