News Groups said:
using Access 2003. How can I delete *all* queries or all reports
from a db? It's easy to import using "select all" from another db,
but how can I remove all other than one by one?
Simplest might be to create a new database and import everything from
the old one except those queries or reports you don't want. That said,
here's a Sub you can use to delete all queries except, optionally, a
list of exceptions you want to preserve.
'----- start of code ----
Sub DeleteQueriesExcept(ParamArray Except() As Variant)
' Delete all queries except those specified in the argument list.
Dim strQueryName As String
Dim intDocX As Integer
Dim intExcluded As Integer
Dim blnDelete As Boolean
With CurrentData.AllQueries
For intDocX = .Count - 1 To 0 Step -1
strQueryName = .Item(intDocX).Name
blnDelete = True ' assume we'll delete this one
For intExcluded = 0 To UBound(Except)
If strQueryName = Except(intExcluded) Then
blnDelete = False
Exit For
End If
Next intExcluded
If blnDelete Then
DoCmd.DeleteObject acQuery, strQueryName
End If
Next intDocX
End With
Application.RefreshDatabaseWindow
End Sub
'----- end of code ----
You'd call it like this:
' delete all queries except Query1
DeleteQueriesExcept "Query1"
' delete all queries except Query1 and Query2
DeleteQueriesExcept "Query1", "Query2"
' delete all queries without exception
DeleteQueriesExcept
Something similar should work with reports (using
"CurrentProject.AllReports" and "acReport" instead of
"CurrentData.AllQueries" and "acQuery"), but you may run into trouble if
the reports have code behind them, since deleting them will affect the
database's VB project.