Use VBA Code to Save class modules imported into Access

I have a vba routine that successfully imports modules (from .cls and .bas files) into my Access 2010 project (.accdb). I am now trying to save those modules using vba.

I have tried this command---Call SysCmd(504, 16483). Although no error occurs, none of the modules is saved.

I have an active "reference" to Microsoft Visual Basic for Applications Extensibility, and I imported the modules using the VBIDE.VBComponents object. Can anyone tell me how to use code to save modules successfully imported?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeCommented:
Look into the

Application.SaveAsText method.

It is not documented in the Access Help, but there are lots of posts here on EE and elsewhere on the web:
PelegrinusAuthor Commented:
Thanks! I am going to look at these in more detail but, based on a quick look, I want to clarify my question: I want to save the modules within the Access application, as if I clicked on the "Save" button in the VB Editor. As I said, I will examine the references you linked in much more detail to see if they answer that question as clarified. Thanks again.
Dale FyeCommented:
Sorry about the confusion.

Actually, you need to look at the LoadFromText method.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rey Obrero (Capricorn1)Commented:
can you clarify this statement

<Can anyone tell me how to use code to save modules successfully imported?>

save to where ?

when you import the modules, they are saved in the access app..
PelegrinusAuthor Commented:
Yes, that does require clarification: When I successfully import the modules, they are visible in the VB Editor with the correct module names. BUT, they are not yet saved. When I click the "Save" button in the VB Editor, or when I close the database, I am prompted to save each module individually. The prompt gives the module's proper name, but I still have to click OK for each of the several dozen modules that I imported. What I want to do is automate that process of saving (and naming) each module.
Rey Obrero (Capricorn1)Commented:
try this codes

Sub importObjects()
Dim db As dao.Database, otherDBPath, j, modObj
otherDBPath = CurrentProject.Path & "\OtherDB.mdb"
Set db = OpenDatabase(otherDBPath)
For j = 0 To db.Containers("modules").Documents.Count - 1
    modObj = db.Containers("modules").Documents(j).Name
        DoCmd.TransferDatabase acImport, "Microsoft Access", otherDBPath, acModule, modObj, modObj

End Sub

change the variables values accordingly
PelegrinusAuthor Commented:
If I understand the code correctly, it assumes that the modules are in another database. I am working on a different model: namely, I'm storing the modules in text-equivalent files (.cls, .bas, etc) in an Explorer directory. I'm doing that in order to have separate text-equivalent files that can be used if my database becomes corrupted, as it sometimes does during development.

And so, having successfully imported these separate .cls and .bas files into working modules in a new database, I want to automate the processing of saving them in the database without having to go through the manual process of "naming" them before they are saved.  

Here's the function I use to import a single module:

Public Function ImportModuleViaVBIDE( _
                            pobjVBC As VBIDE.VBComponents, _
                            pstrPath As String, _
                            pstrFile As String) As Boolean
    ' Comments: This processes a single code file in a directory designated _
                in the calling routine. _
                --  Determine whether the code already exists in pObjVBC _
                --  If so, delete the code and _
                            import the file in place of it _
                --  If not, import the code
    ' Params  :
    ' Returns : Boolean
    ' Created : 04/05/12 16:08 JV
    ' Modified:
    'TVCodeTools ErrorEnablerStart
    On Error GoTo PROC_ERR
    'TVCodeTools ErrorEnablerEnd
    Dim strFName                As String
    Dim lngT                    As Long
    strFName = FullPathAndFileName(pstrFile, "", pstrPath) ' code not included in this sample
    '   *****   if the component exists, delete it
    For lngT = pobjVBC.Count To 1 Step -1
        If pobjVBC(lngT).Name = Left(pstrFile, InStr(1, pstrFile, ".") - 1) Then
            pobjVBC.Remove pobjVBC(lngT)
        End If
    Next lngT
    pobjVBC.Import strFName
    ImportModuleViaVBIDE = True

    'TVCodeTools ErrorHandlerStart
    Exit Function

    MsgBox Err.Description, vbCritical, "modModulesImportExport.ImportModuleViaVBIDE"
    Resume PROC_EXIT
    'TVCodeTools ErrorHandlerEnd

End Function

At this point, the module is visible in the VB Editor and can be edited and executed. It's not saved, though, and that's the step I want to automate.
Rey Obrero (Capricorn1)Commented:
ooh, you must use what fyed proposes "application.loadfromtext"

the syntax is

Application.LoadFromText acmodule,"NameOfModule", "c:\folder\ModuleName.bas"
PelegrinusAuthor Commented:
That goes part way there, but Application.LoadFromText does not distinguish between standard modules and class modules. I loaded a class modue (and it did not need to be saved after the load was complete), but it is stored under modules rather than class modules.  And the header contains language above the permissible option statementsa that (understandably) will not compile:

  MultiUse = -1  'True

It seems as if a different approach is required for a solution.
PelegrinusAuthor Commented:
I can't believe that I'm the first to run into this stone wall. I have the impression that it's a common practice to export modules to .cls or .bas files and then import them into new databases (and keep the distinction between standard modules and class modules).

I can easily automate that process. There must be some way also to automate the required step of SAVING the modules that have been imported.

I've looked in Application.VBE.ActiveVBProject.VBComponents and in Application.CurrentProject but have not seen anything that solves my question. (CurrentProject.UpdateDependencyInfo will save the module put requires that I manually confirm the module name for each of the many modules I imported.) I REALLY would appreciate any specific suggestions.
You should be able to do the following after you import the module...

DoCmd.Save acModule

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"I have the impression that it's a common practice to export modules to .cls or .bas files and then import them into new databases (and keep the distinction between standard modules and class modules)."

Well, not really. It's pretty simple to just Import into a new db container. Access handles that just fine, including maintaining the distinction between Class modules and Standard modules.

The non-standard part of what you are doing seems to be that you are doing this as part of a run-time process.  

(sent from phone)
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Hey Brent ... if you have time, pop over to this Q and see if you have an thoughts ... on the limitations and issues.

PelegrinusAuthor Commented:
"Well, not really. It's pretty simple to just Import into a new db container. Access handles that just fine, including maintaining the distinction between Class modules and Standard modules."

The reason I'm experimenting with .cls and .bas files stored in an Explorer directory is to "fix" a corrupted database where the corruption occurs during my development process. Although I have a lot of experience with vba coding, this part is new for me. If exporting the code to a new database container will "clean" the code, then I'd be glad to do that, because I agree (in concept, at least) that taking that route would be simpler. Does my concern about "fixing" corrurpted code make sense?
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"Does my concern about "fixing" corrurpted code make sense?"
First ... are you familiar with Decompile ?

PelegrinusAuthor Commented:
This is the solution. I now do the equivalent of this code, where the mcstr variables identify the module at issue:

    Application.VBE.ActiveVBProject.VBComponents.Import mcstrImportExportModule
    Application.DoCmd.Save acModule, mcstrModuleName
Rey Obrero (Capricorn1)Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.