MS Access VBA Delete all Tables For versions 2000, 2002, 2003, 2007
Go here To
Delete Records in the Query Builder.
Go here To
Delete a Table in VBA.
Go here To
Delete a Field in VBA.
Go here To
Delete Records in VBA.
The VBA
Delete All Tables Sample is available for download
here in a zipped MS Access Database. The below picture (click to
enlarge) shows the form.
The Actual VBA Code to Delete all Tables you
should be able to copy/paste right into a module:
Public Sub
DeleteAllTables()
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object
Library
'Set references by Clicking Tools and Then References in the Code
View window
'DELETES all Table Names that exist in The Current Database and
excludes tables
'with MSys and ~.
' Accepts
' Nothing
' Returns All your Table Names and excludes tables with MSys and ~
'USAGE: GetAllTables
On Error GoTo
errhandler
'Capture any errors.
Dim
i
As Integer
, X
As Integer
, numTables
As Integer
, tabName
As String
, boolInt
As
Integer
Dim
numList
As Integer
'Set the controls focus to print to
Form_DeleteAllTables_Form.lstTblNames.SetFocus
If
Form_DeleteAllTables_Form.lstTblNames.RowSourceType <>
"Value List"
Then
Form_DeleteAllTables_Form.lstTblNames.RowSourceType =
"Value List"
' Loop through all the Tables
numTables = CurrentDb.TableDefs.Count -
1
For
i =
0
To
numTables
'Set to 0 to skip bad tables
boolInt =
0
'set string to the Table Name
tabName = LCase(CurrentDb.TableDefs(i).Name)
'Check for ~ and mssys prefixed Tables and skip them
If
InStr(
1
, tabName,
"msys"
) =
False And
_
InStr(
1
,
tabName,
"~"
) =
False Then
'Set the controls focus to print to
Form_DeleteAllTables_Form.lstTblNames.SetFocus
'Check for first listitem
If
IsNull(Form_DeleteAllTables_Form.lstTblNames.Column(
0
,
0
)) =
False Then
'Cycle through the listitems ensure the new item is not duplicated
numList
= Form_DeleteAllTables_Form.lstTblNames.ListCount -
1
For
X =
0
To
numList
'Check for Listitem duplicate
If
Form_DeleteAllTables_Form.lstTblNames.Column(
0
, X) = tabName
Then
'Set to 0 to skip duplicate
boolInt =
0
Else
'Set to 1 to Add item
boolInt =
1
End If
Next
X
Else
'Set to 1 for Additem Check
boolInt
=
1
End If
'if boolint 1 then add the item
If
boolInt =
1
Then
Form_DeleteAllTables_Form.lstTblNames.AddItem tabName
End If
End If
Next
i
'I chose a list to store the tables and then delete them.
'Originally the above code was set to delete the tables
'instead of adding them to a list. This failed and always
'skipped one or two tables.
'Cycle through are list of tables to delete
For
X =
0
To
Form_DeleteAllTables_Form.lstTblNames.ListCount -
1
'If item is not null delete it
If
IsNull(Form_DeleteAllTables_Form.lstTblNames.Column(
0
, X)) =
False Then
'Call the Delete Table Function
'Be Sure to grab the
DeleteTable Module
DeleteTable
Form_DeleteAllTables_Form.lstTblNames.Column(
0
, X)
End If
Next
Exit Sub
errhandler:
'Capture any Errors
With
Err
MsgBox
"Error " &
.Number
&
vbCrLf
&
.Description, _
vbOKOnly
Or
vbCritical,
"DeleteAllTables"
End With
End Sub
|
|
For the above VBA Delete All Tables Sample with usable Form and Button,
download the Sample database in ZIP format. The sample includes 4
tables in which to test on.
You may post this tutorial on your website or in a forum. If you do please
maintain a Link to Eraseve AP.
For further MS Access tutorials go here:
MS Access tutorials
Go here To
Delete a Table in VBA.