Problems importing a module using VBA

aka_BigRed

New member
Local time
Today, 07:46
Joined
Aug 29, 2007
Messages
7
I have a little automatic updater that checks a central server location and imports new versions of tables/queries/forms, etc to keep all instances of my MS Access database in sync. It has been working flawlessly.

I decided to rework some code and make some global functions in a separate module. For whatever reason, I can't seem to get the modules to import with the correct name. First it deletes the old object and then it imports the new one. The only problem is it won't use the correct name, it somehow thinks the original still exists (after being deleted) and renames the new import to oldModule1

How do I remove the module programmatically using VBA so that when it imports the new copy with same name it keeps the name instead of renaming?




Module to import: someModule

It runs, deletes someModule, imports the new version of someModule from the server but then still renames even though there's no naming conflict.

After running, someModule1 exists, and someModule does not exist

Help! I need a solution to roll this out ASAP!!

example code snippet:
PHP:
      If AddEdit = "Edit" Then
        place = "ErrDelete"
        If TypeChg = "query" And ObjectExists(acQuery, NameChg) Then
            DoCmd.DeleteObject acQuery, NameChg
        End If
        If TypeChg = "Table" And ObjectExists(acTable, NameChg) Then
            DoCmd.DeleteObject acTable, NameChg
        End If
        If TypeChg = "Form" And ObjectExists(acForm, NameChg) Then
            DoCmd.DeleteObject acForm, NameChg
        End If
        If TypeChg = "Report" And ObjectExists(acReport, NameChg) Then
            DoCmd.DeleteObject acReport, NameChg
        End If
        If TypeChg = "Macro" And ObjectExists(acMacro, NameChg) Then
            DoCmd.DeleteObject acMacro, NameChg
        End If
''''''''''''''''''''''''''''''''''''''''
      If TypeChg = "module" And ObjectExists(acModule, NameChg) Then
            DoCmd.DeleteObject acModule, NameChg
        End If
''''''''''''''''''''''''''''''''''''''''
    End If
    

''Imports the new/edited table, query, etc from the master file on server
    place = "Import"
    If TypeChg = "query" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acQuery, NameChg, NameChg
    End If
    If TypeChg = "table" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acTable, NameChg, NameChg
    End If
    If TypeChg = "form" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acForm, NameChg, NameChg
    End If
    If TypeChg = "report" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acReport, NameChg, NameChg
    End If
    If TypeChg = "macro" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acMacro, NameChg, NameChg
    End If
''''''''''''''''''''''''''''''''''''''''
   If TypeChg = "module" Then
        DoCmd.TransferDatabase acImport, "Microsoft Access", _
        masterloc, acModule, NameChg, NameChg
    End If
''''''''''''''''''''''''''''''''''''''''
 
Well, I can't seem to force it to do what I want directly (ie importing with correct name) so I created a hack to rename any modules with a trailing "1" in their name basename.

For whatever reason, renaming the module in the autoexec form after it had been referenced seemed to crash MS access, so this little snippet has to be the first thing done *BEFORE REFERENCING* the module to be renamed. It worked fine if I was already in access and just opened the updater form, but if I exited & then re-opoened DB so the form ran automatically as autoexec, it crashed access. Same code, catastrophically different results - go figure.

Granted, for the first use of the DB where a module update is imported from the central server the module name has a trailing "1", but the next time the user opens the DB, this code renames to the correct name

It's not pretty, but Its a workable solution :)

Code:
Dim I As Integer, fixedModuleName As String, origModuleName As String

''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' This section is a hack/workaround because when importing modules using version updater, Access wants to add a
'' trailing "1" at the end of the name.  This checks all modules and if they end with a "1" it removes it and renames.
'' After the first import, they will run fine with the trailing "1" in the name, but on next toolupdate check it will
'' rename to correct name so there's never a duplicate when an updated module is pushed by version updater.
'' This check/rename must be done as the VERY FIRST THING on startup or it was causing Access to crash if done later
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
  For I = 0 To CurrentDb.Containers("Modules").Documents.Count - 1
     
     origModuleName = CurrentDb.Containers("Modules").Documents(I).Name
     
     'Determine if the Module ends in a "1" suggesting it was imported last run
     If (Mid(origModuleName, Len(origModuleName), 1) = "1") Then
        'remove the trailing "1" in name then rename
        fixedModuleName = Mid(origModuleName, 1, Len(origModuleName) - 1)
        DoCmd.Rename fixedModuleName, acModule, origModuleName
     End If
   Next I
''''''''''''''''''''''''''''''''''''''''''''''''''''''

{... rest of code to import new versions goes here below ...}
 

Users who are viewing this thread

Back
Top Bottom