Delete an object in another database


Question

This question was in the archives with no answer. How do you delete an object in another database?

Answer

From: Shamil Salakhetdinov <shamil@marta.darts.spb.ru>
To: ACCESS-L <ACCESS-L@PEACH.EASE.LSOFT.COM>
Subject: Re: Delete an object in another database
Date: 14 July 1998 1:59

Walter,

I guess the question is about the deletion of forms,reports,macros and modules?
Here is a template function which uses (OLE-)Automation to delete them in an external
mdb file.

Public Function ExtObjsDelAll(ByVal vstrMdbPath As String)
    Dim app As New Access.Application
    Dim con As Container
    Dim doc As Document
    Dim qdf As QueryDef
    Dim lngObjType As Long
    
    app.OpenCurrentDatabase vstrMdbPath
    
    For Each con In app.CurrentDb.Containers
      Select Case con.Name
      Case "Tables", "Forms", "Reports", "Scripts", "Modules":
        For Each doc In con.Documents
         If Left(doc.Name, 4) <> "Msys" And _
            Left(doc.Name, 4) <> "Usys" And _
            Left(doc.Name, 1) <> "~" Then
              
              Select Case doc.Container
              Case "Tables":
                   On Error Resume Next
                   Set qdf = app.CurrentDb.QueryDefs(doc.Name)
                   If Err <> 0 Then
                      lngObjType = acTable
                   Else
                      lngObjType = acQuery
                   End If
                   On Error GoTo 0
              Case "Forms":   lngObjType = acForm
              Case "Reports": lngObjType = acReport
              Case "Scripts": lngObjType = acMacro
              Case "Modules": lngObjType = acModule
              Case Else:      lngObjType = -1
              End Select
              
              If lngObjType <> -1 Then
                 app.DoCmd.DeleteObject lngObjType, doc.Name
                 Debug.Print doc.Name & " - deleted."
              End If
         End If
        Next
      Case Else
      End Select
    Next
    
    app.Quit
    
    Set doc = Nothing
    Set con = Nothing
    Set qdf = Nothing
    Set app = Nothing
End Function

HTH,
Shamil


HOME    TOPICS

Copyright © 1998–1999 by Shamil Salakhetdinov.
All rights reserved. Terms of use.

Last updated: October 10, 2006

Published also here at 4TOPS: Delete an object in another database