Create Module Through Code

  • Thread starter Thread starter Robert Taylor
  • Start date Start date
R

Robert Taylor

Does anyone know if there is a way to create a module in a
database through code?

I know how to open existing modules and write to them in
VBA using the OpenModule and the Addfromfile/addfromstring
methods, but I really want a technique to create a new
module in code and then send the code I want in it.

Thanks.

Robert
 
I know how to open existing modules and write to them in
VBA using the OpenModule and the Addfromfile/addfromstring
methods, but I really want a technique to create a new
module in code and then send the code I want in it.

You need to set a reference to VBA Extensibility library and use
VBComponents assuming you are using Access 2000 or better.

' *** Code Start ***
Sub ModuleTest()
Dim acc As Access.Application
Dim mdl As VBComponent

Set acc = New Access.Application
With acc
.Visible = True
.OpenCurrentDatabase "C:\personal\My Documents\db2.mdb", False
Set mdl = .VBE.ActiveVBProject.VBComponents.Add
(vbext_ct_StdModule)
mdl.Name = "MyModule"
Stop
End With
End Sub

'*** Code End ***

-- Dev
 
Back
Top