Solved

Use VBA Code to Save class modules imported into Access

Posted on 2012-04-05
18
1,731 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)
ID: 37815707
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
ID: 37815885
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)
ID: 37815960
Sorry about the confusion.

Actually, you need to look at the LoadFromText method.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37815995
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
ID: 37816143
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37816173
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
ID: 37816932
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37817007
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
ID: 37817078
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
 

Author Comment

by:Pelegrinus
ID: 37819182
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
ID: 37819369
You should be able to do the following after you import the module...

DoCmd.Save acModule
0
 
LVL 75
ID: 37819590
"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
ID: 37819978
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
ID: 37820033
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
ID: 37820149
"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
ID: 37820369
"Does my concern about "fixing" corrurpted code make sense?"
First ... are you familiar with Decompile ?

mx
0
 

Author Closing Comment

by:Pelegrinus
ID: 37820372
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37820643
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

832 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