Importing Modules (1 Viewer)

grahamvb

Registered User.
Local time
Today, 07:13
Joined
Aug 27, 2013
Messages
57
Hello Access-Programmers,

I am using Access 2010 and trying to import objects (not tables) from another (closed) .accdb into an open .accdb. I can count the module objects with CurrentProject.AllModules.Count.

I need to read the modules names so I can try to import them using DoCmd.TransferDatabase. I think I will also need to delete existing objects with the same name before I can write the new ones.

In the end, I would like to be able to import Queries, Forms, Reports, Data Access Pages and Macros in addition to Modules.

The immediate question is how to read object (Module) names from an external (Not open) db.

Any help is appreciated
 

Cronk

Registered User.
Local time
Today, 21:13
Joined
Jul 4, 2013
Messages
2,772
You need to set a reference to the other database, something like

Dim myApp As Application
Dim n As Integer

Set myApp = CreateObject("Access.Application")
myApp.OpenCurrentDatabase "Path & File name", False
Dim strModuleName(myApp.Modules.Count)
For n = 0 To myApp.Modules.Count - 1
strModuleName(n) = myApp.Modules(n).Name
Next n
 

grahamvb

Registered User.
Local time
Today, 07:13
Joined
Aug 27, 2013
Messages
57
Thank you for getting me started. Here is where I am now.

This part works great counting, reading and listing the CurrentProject's VBA Module's names.

Code:
'   Use the "CurrentProject" collections to get the counts of objects (This db)
Dim varCurrentModuleCount As Integer
Dim n1 As Integer
 
    With CurrentProject
        varCurrentModuleCount = .AllModules.Count
        Debug.Print "--------------------------------------"
        Debug.Print "CurrentProject.AllModules.Count: " & varCurrentModuleCount
        Debug.Print "--------------------------------------"
    End With
 
Dim strCurrnntDBModuleName()
ReDim strCurrentDBModuleName(varCurrentModuleCount)
 
    For n1 = 0 To varCurrentModuleCount - 1
        strCurrentDBModuleName(n1) = CurrentProject.AllModules.Item(n1).Name
        Debug.Print "CurrentProject.Application.Modules.Item.Name: " & strCurrentDBModuleName(n1)
    Next n1

This part however, reads some (not all) Form_Names from the other db. The quantity (varOtherModuleCount) does not match the number of VBA Modules in the other db and the list of objects (strOtherModuleName(n2)) is a partial list of forms.

Code:
'   FileLocUpdate is the path to the external update file (to be read) (FileLocUpdate Public variable passed here)
FileLocUpdate = "C:\Users\NAME\Desktop\Test\Other.accdb" ' Remove this line from finished db
 
'   Open Other db
Dim UpdateDB As Application
Set UpdateDB = CreateObject("Access.Application")
'   opens other db as it should
    UpdateDB.OpenCurrentDatabase FileLocUpdate
 
'   Read Other db
Dim varOtherModuleCount As Integer
Dim n2 As Integer
Dim strOtherModuleName()
ReDim strOtherModuleName(UpdateDB.Modules.Count)
    varOtherModuleCount = UpdateDB.Modules.Count            ' reads quantity of 2
 
        Debug.Print "--------------------------------------"
        Debug.Print "Update.accdb Modules: " & varOtherModuleCount
        Debug.Print "--------------------------------------"
 
    For n2 = 0 To varOtherModuleCount - 1
        strOtherModuleName(n2) = UpdateDB.Modules(n2).Name  ' reads 2 Form_Names
        Debug.Print strOtherModuleName(n2)                  ' One opens on startup, one not open
    Next n2
 
'------------------------------------------------------
' This reads CurrentProject (This db) even with other Application (db) Open
'------------------------------------------------------
'    With CurrentProject
'        varOtherModuleCount = .AllModules.Count
'        Debug.Print "--------------------------------------"
'        Debug.Print "OtherProject.AllModules.Count: " & varOtherModuleCount
'        Debug.Print "--------------------------------------"
'    End With
'    Dim strOtherDBModuleName()
'    ReDim strOtherDBModuleName(varOtherModuleCount)
'    Debug.Print Modules(0)
'    For A = 0 To varModuleCount - 1
'        strOtherDBModuleName(A) = CurrentProject.AllModules.Item(A).Name
'        Debug.Print "OtherProject.Application.Modules.Item.Name: " & strOtherDBModuleName(A)
'    Next A
'-------------------------------------------------------
UpdateDB.CloseCurrentDatabase ' closes other db as it should

Any ideas on how to read the actual modModuleNames from the external db?
 
Last edited:

Cronk

Registered User.
Local time
Today, 21:13
Joined
Jul 4, 2013
Messages
2,772
Graham

Sorry, I can't help further. I've never needed to go beyond counting lines in modules.

I'd be grateful thought to learn if you do get something that reads lines in procedures.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:13
Joined
Jan 20, 2009
Messages
12,853
To read the names of the procedures in the module you would need to find lines that include "Sub " or "Function " etc and parse them appropriately. Do this with a with a loop through the Lines Property of the Module.

Once you know the names of the procedures their individual lines can be addressed with properties such as ProcBodyLine etc
 

Users who are viewing this thread

Top Bottom