René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

write_to_module [Access macro]

' http://www.cpearson.com/excel/vbe.aspx
' be sure to first run insert_module.bas
' run delete_sub afterwards

option explicit

public sub write_to_module()

  dim ac        as access.application
' dim db        as dao.database

  dim vb_editor as vbide.vbe
  dim vb_proj   as vbide.vbProject
  dim vb_comp   as vbide.vbComponents
  dim vb_module as vbide.vbComponent
  dim vb_code   as vbide.codeModule

  dim mdb_name  as string
      mdb_name = "c:\temp\inserted_module.mdb"

  dim module_name as string
      module_name = "ThisModuleWasInserted"

  set ac = new access.application
  ac.openCurrentDatabase(mdb_name)
' set db = ac.currentDB

  set vb_editor = ac.vbe
  set vb_proj   = vb_editor.activeVBProject
  set vb_comp   = vb_proj.vbComponents

  set vb_module = vb_comp(module_name)

  set vb_code = vb_module.codeModule()

  vb_code.addFromString("sub Foo()" & vbCrLf & "  MsgBox(""In foo"")" & vbCrLf & "End Sub")
  vb_code.addFromString("sub Bar()" & vbCrLf & "  MsgBox(""In Bar"")" & vbCrLf & "End Sub")
  vb_code.addFromString("sub Del()" & vbCrLf & "  MsgBox(""In Bar"")" & vbCrLf & "  ' to be deleted" & vbCrLf & "End Sub")
  vb_code.addFromString("sub Sum()" & vbCrLf & "  MsgBox(""In Sum"")" & vbCrLf & "End Sub")

end sub