Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Burgas, Bulgaria
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete all modules (Access 2000)

    I have asked the Forum how to import all modules with a single code.The code suggested by Hans is marvelous and
    i apply it already.I need now, after i finish with the job, to delete all modules.May i ask also for help,
    since there is function of importing all modules,it should be also possible to delete all modules in the current
    database,

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Delete all modules (Access 2000)

    I'm not going to ask why you want to do this, but here is sample code:

    Public Sub DeleteAllModules()
    On Error GoTo Err_Handler

    Dim db As DAO.Database
    Dim doc As DAO.Document
    Dim n As Long
    Dim strMsg As String

    Set db = CurrentDb

    For Each doc In db.Containers("Modules").Documents
    DoCmd.DeleteObject acModule, doc.Name
    n = n + 1
    Next doc

    MsgBox n & " modules deleted.", vbInformation, "TEST MSG"

    Exit_Sub:
    Set db = Nothing
    Set doc = Nothing
    Exit Sub
    Err_Handler:
    strMsg = "Error No " & Err.Number & ": " & Err.Description
    Beep
    MsgBox strMsg, vbExclamation, "ERROR MESSAGE"
    Resume Exit_Sub
    End Sub

    Note: Tested this from current database, successfully deleted all code modules (including the one with the code to delete the modules - it stupidly deleted itself.) You can modify if necessary to delete all modules in another database by setting db object variable to another database using DAO OpenDatabase method.

    HTH

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete all modules (Access 2000)

    Deleting ALL modules in the current database is dangerous, because you would also delete the module that is running the code, unless you put the code in the module belonging to a form. So be careful! Here is the code:

    Sub DeleteModules()
    Dim dbs As DAO.Database
    Dim ctr As DAO.Container
    Dim i As Integer

    On Error GoTo ErrHandler

    Set dbs = CurrentDb
    Set ctr = dbs.Containers("Modules")
    For i = ctr.Documents.Count - 1 To 0 Step -1
    DoCmd.DeleteObject acModule, ctr.Documents(i).Name
    Next i

    ExitHandler:
    On Error Resume Next
    Set ctr = Nothing
    dbs.Close
    Set dbs = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    If you want to delete the modules in another database, it is easy to change this procedure to take a strDatabaseName argument, as in the procedure to transfer modules.

  4. #4
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Burgas, Bulgaria
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you

    Thank you for the wonderful suggestions the codes are fllying now !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •