Solved

Use VBA Code to Save class modules imported into Access

Posted on 2012-04-05
18
1,665 Views
Last Modified: 2012-04-07
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?
0
Comment
Question by:Pelegrinus
  • 7
  • 4
  • 3
  • +2
18 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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:

http://blogs.msdn.com/b/thirdoffive/archive/2006/08/31/698640.aspx

http://allenbrowne.com/ser-47.html
0
 

Author Comment

by:Pelegrinus
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Sorry about the confusion.

Actually, you need to look at the LoadFromText method.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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..
0
 

Author Comment

by:Pelegrinus
Comment Utility
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.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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
        Application.RefreshDatabaseWindow
Next

db.Close
End Sub


change the variables values accordingly
0
 

Author Comment

by:Pelegrinus
Comment Utility
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
PROC_EXIT:
    Exit Function

PROC_ERR:
    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.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
ooh, you must use what fyed proposes "application.loadfromtext"

the syntax is

Application.LoadFromText acmodule,"NameOfModule", "c:\folder\ModuleName.bas"
0
 

Author Comment

by:Pelegrinus
Comment Utility
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:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
End

It seems as if a different approach is required for a solution.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Pelegrinus
Comment Utility
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.
0
 
LVL 11

Accepted Solution

by:
datAdrenaline earned 500 total points
Comment Utility
You should be able to do the following after you import the module...

DoCmd.Save acModule
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"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.

mx
0
 
LVL 11

Expert Comment

by:datAdrenaline
Comment Utility
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)
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Hey Brent ... if you have time, pop over to this Q and see if you have an thoughts ... on the limitations and issues.

Here

thx.mx
0
 

Author Comment

by:Pelegrinus
Comment Utility
"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?
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"Does my concern about "fixing" corrurpted code make sense?"
First ... are you familiar with Decompile ?

mx
0
 

Author Closing Comment

by:Pelegrinus
Comment Utility
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
   
    Application.RefreshDatabaseWindow
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
:-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now