Yes, it was open in instances hidden to my view as I worked with the
application objects in the VBA window. I had to close the open MSAccess
applications in the Task Manager "Processes" window to unlock the target DB.
I also needed to use the Db.Properties.Refresh method to get this sub to
work, maybe because the MSysObjects table was changing with each deletion?
Thanks to your help, here is what I came up with (generic object names
inserted), which is working fine:
Sub CleanDatabase(FileName As String)
Dim app As Application
Set app = CreateObject("access.application")
app.OpenCurrentDatabase (FileName)
With app.DBEngine.OpenDatabase(FileName).OpenRecordset( _
"SELECT Name,IIf(Type = 1, 0, IIf(Type = 5, 1, IIf(Type = -32768, 2,
3))) " & _
"AS ObjectType " & _
"FROM MSysObjects " & _
"WHERE (Type = -32764 And Not Name In
('rptSample1','rptSample2',*etc*)) " & _
"Or (Type = -32768 And Not Name In ('frmSample1','frmSample2',*etc*))
" & _
"Or (Type = 1 And Not (Name Like '*MSys*' Or Name In
('tblSample1',*etc*))) " & _
"Or (Type = 5 And Not (Name Like '~sq*' Or Name In
('qrySample1',*etc*)))")
While Not .EOF
app.DoCmd.DeleteObject !ObjectType, !Name
app.CurrentDb.Properties.Refresh
.MoveNext
Wend
.Close
End With
app.CloseCurrentDatabase
Set app = Nothing
End Sub