I'm working an Access add-in that needs to be able to selectively import code from other Access applications into the CurrentDb (not the add-in)
I've already written code that will identify the procedures which need to be imported from the external database, and locate them for copying. The next step is to create a new module in the CurrentDb (not the CodeDb) to put the code from the source database in.
I found this thread
which discusses using docmd.runcommand acCmdNewObjectModule and after some additional searching, found this link
which contained some sample code, which I converted to:
Public Function CreateModule(ModuleName As String) As Boolean
Dim db As DAO.Database
Dim doc As Document
Dim NewMod As Module
On Error GoTo ProcError
Set doc = Currentdb.Containers("Modules").Documents(ModuleName)
CreateModule = True
Set db = Nothing
If Err.Number = 3265 Then
Set NewMod = Application.Modules.Item(Application.Modules.Count - 1)
DoCmd.Save acModule, NewMod.Name
DoCmd.Close acModule, NewMod.Name, acSaveYes
DoCmd.Rename ModuleName, acModule, NewMod.Name
CreateModule = True
MsgBox Err.Number & vbCrLf & Err.Description, , "CreateModule"
Debug.Print "CreateModule", Err.Number, Err.Description
That worked fine when the add-in was run as a stand-alone application, but when run as an add-in it generated error:
#2046 'The command or action 'NewObjectModule' isn't available now.
I've toyed around with various alternatives of this with no success.
I then stumbled upon this thread
from back in 2006, which uses Microsoft Visual Basic for Applications Extensibility, but I was unable to get that to work at all.
I then found a suggestion to use the LoadFromText and found this thread
but when I run the LoadFromText code I get error #2050 'Reserved Error' and when I did some more digging, I found that SaveAsText and LoadFromText are not available in Sandbox mode, which I guess means they won't work in Access 2013.
So, now I'm back to square one. Anybody have any ideas that will work across all versions of Access from 2003 to 2013, as an Add-In?