Delete remote module

jukos

New member
Local time
Today, 15:54
Joined
Dec 15, 2017
Messages
9
Deleting a module in remote database using vba is working using this code :

Code:
Dim objAcc As Access.Application
Set objAcc = GetObject("\\grh503\Electronics\database_programs\z_rest_of_em\tools\golive\GoLive.accdb")
objAcc.DoCmd.DeleteObject acModule, "tools-for_all_db_123"
objAcc.Application.Quit
Set objAcc = Nothing

BUT... I wont know what last 3 characters will be in the module name "tools-for_all_db_123" . I would like to delete the module starting with "tools-for_all_db"

Thanks all!
 
Just off the top of my head, maybe you could use the MSysObjects table to get those names?
 
You can enumerate the AllModules collection in a loop to do something like

Code:
For n = 0 to objAcc.AllModules.Count - 1
   xxx = objAcc.AllModules(n).Name
   if Left(xxx, 15 ) = "tools-for_all_db_" then ...
   ...
Next n

in a loop to find the module where the first part of the name matches your goal. This works because that is one of the ways iterate through collections. When you use AllModules it actually defaults to the current DB, but with a prefix to qualify which DB you meant, it should work.
 
I tried this code to test but getting compile error:
Annotation 2023-06-30 103448.jpg
 
objAcc.CurrentProject.AllModules

Tip: If you want to delete multiple times within the loop, I would run the loop backwards.
 
To get a list of modules....

Code:
Sub ListModulesInExtDb()
    Dim strFilePath As String, obj As Object
    Dim objAcc As Access.Application
    
    strFilePath = "Full file path here"
    Set objAcc = GetObject(strFilePath)
    
    For Each obj In objAcc.CurrentProject.AllModules
        Debug.Print obj.Name
    Next
    
    Set objAcc = Nothing
End Sub

Definitely follow Josef's advice if you need to do multiple deletes or results may not be what you expect
 
Whoops... it has been so long that I did that, I forgot the "CurrentProject" component. My bad.
 

Users who are viewing this thread

Back
Top Bottom