Deleting All Reports using VBA

  • Thread starter rwboyden via AccessMonster.com
  • Start date
R

rwboyden via AccessMonster.com

I'm using the following code to try to delete all the reports in the current
database. It doesn't work. What am I missing? Many thanks

Dim obj As AccessObject
Dim dbs As Object
Set dbs = Application.CurrentProject
For Each obj In dbs.AllReports
If obj.IsLoaded = True Then
strRpt = obj.Name
DoCmd.DeleteObject acReport, strRpt
DoEvents
End If
Next obj
 
K

Ken Snell MVP

Be careful. Once deleted, the report is gone.

Dim lngCount As Long
With Application.CurrentProject
For lngCount = .AllReports.Count - 1 To 1 Step -1
DoCmd.DeleteObject acReport, .AllReports(lngCount).FullName
Next lngCount
End With
 
M

Marshall Barton

rwboyden said:
I'm using the following code to try to delete all the reports in the current
database. It doesn't work. What am I missing? Many thanks

Dim obj As AccessObject
Dim dbs As Object
Set dbs = Application.CurrentProject
For Each obj In dbs.AllReports
If obj.IsLoaded = True Then
strRpt = obj.Name
DoCmd.DeleteObject acReport, strRpt
DoEvents
End If
Next obj


The AllReports (and any other collection) will be rearranged
when you delete an entry. That means that what used to be
the second item becomes the first item. But the For each
loop goes on to the second item, effective skipping the new
first item.

You can do what you want by deleteing from the end of the
collection:

Your code is only deleting open (loaded) reports. Is that
really the objective?
 
J

John Spencer MVP

One very minor correction. I believe the line should be
For lngCount = .AllReports.Count - 1 To 0 Step -1
so that ALL the reports will get deleted. Ken's version should leave one
report undeleted.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
R

rwboyden via AccessMonster.com

Ken said:
Be careful. Once deleted, the report is gone.

Dim lngCount As Long
With Application.CurrentProject
For lngCount = .AllReports.Count - 1 To 1 Step -1
DoCmd.DeleteObject acReport, .AllReports(lngCount).FullName
Next lngCount
End With
I'm using the following code to try to delete all the reports in the
current
[quoted text clipped - 10 lines]
End If
Next obj

Ken, this works very nicely. Many thanks ... Bob Boyden
 
R

rwboyden via AccessMonster.com

John said:
One very minor correction. I believe the line should be
For lngCount = .AllReports.Count - 1 To 0 Step -1
so that ALL the reports will get deleted. Ken's version should leave one
report undeleted.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
Be careful. Once deleted, the report is gone.
[quoted text clipped - 4 lines]
Next lngCount
End With

Thanks, John.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top