Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create Module & save it

Status
Not open for further replies.

MichaelRed

Programmer
Dec 22, 1999
8,410
0
0
US
Thread 705-153380 provided the function code below. I know it worked when posted for Ms. A. 2K. I am trying to use it I Ms. A 2003 and get a failure on the line indicated (to SAVE the newly created object). The error is :

Debug.Print err, Error(Err)
29068 Microsoft Office Access cannot complete this operation. You must stop the code and try again.

I have checked that each term in the statement is 'valid' in Ms. A 2003, as attempting a few work arounds (moving the save & subsequent commands to a separate function which is called seperatly ...) all to no avail.

I KNOIW it has worked, as I tried it when the original soloution was posted) Any one have an updated version of this, or suggestion(s)?

Code:
Public Function basModule_Make_II(strModName As String) As Boolean

    Dim mdl As Module
    Dim blnModSave As Boolean

    DoCmd.SetWarnings False

        ' Create the module.
        DoCmd.RunCommand acCmdNewObjectModule

        ' Set MyModule to be the new Module Object.
        Set mdl = Application.Modules.Item(Application.Modules.Count - 1)
        ' Save, close, and rename the new Module.
        DoCmd.Save acModule, save, mdl      ' Error HERE
        DoCmd.Close acModule, mdl, acSaveYes
        DoCmd.Rename strModName, acModule, mdl
        DoCmd.OpenModule strModName
        Set mdl = Modules(strModName)

    DoCmd.SetWarnings True

End Function


MichaelRed


 
There is a slight trick...
1. You can't have any unsaved modules open. That means take the following code, place in a new module, and SAVE IT.
2. Place the cursor inside the 'test-it' function, then hit F5. You can NOT step through the code line by line.

Try the following code:
Code:
Function test_it()
    MyNewModule2000 ("WAYNE")
End Function
Function MyNewModule2000(pModName As String)
'Purpose:   Programatically create and
'           name a new module in the
'           current db.
Dim MyModule As Module

' Create the module.
DoCmd.RunCommand acCmdNewObjectModule

 ' Set MyModule to be the new Module Object.
 Set MyModule = Application.Modules.Item(Application.Modules.Count - 1)

' Save, close, and rename the new Module.
DoCmd.Save acModule, MyModule
DoCmd.Close acModule, MyModule, acSaveYes
DoCmd.Rename pModName, acModule, MyModule
End Function

Sometimes computer problems are like genealogy... The answer to one problem leads to two more!
 
Thanks. A lot.

This 'trick' works as you state.

Give that my project might (need?) to create a number of modules, it seems reasonable to consider placing the necessary & sufficient instructions (as code) directly in the module.

This appears to be a valid approach,

DoCmd.RunCommand acCmdSaveAllModules

... but a 5AM I can't quite get there.

? err, error(err)
2046 The command or action 'SaveAllModules' isn't available now.

The 'documentation' mentions the acCmd* group is available as the Menu items. I admit I cannot find this on any of the 'standard' menus. Alas, alack & awry perhaps a clearer head will appear some other day / time.

Thanks - again. If you would care to share a bit more, I would appreciate a reference to the documentation of your solution. Perhaps that would lead me to even more understanding.




MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top