<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: vba code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 28 Aug 2008 05:12:12 GMT</pubDate>
    <description>DZone Snippets: vba code</description>
    <item>
      <title>Add lines of code to module in design view using VBEIDE</title>
      <link>http://snippets.dzone.com/posts/show/5436</link>
      <description>Sometimes you'll need to add code programmatically to your Form or Report Module, for example, when you need to add the same code to all controls of a certain type on a form. When the form has a handful of controls it's no big deal, but when there may be dozens or hundreds of controls it can be daunting. The code below will cycle through all of the controls on your form, determine if the control type has to be updated, check to see if the control already has an event defined, and if not, create the event and append the code. Otherwise it'll print the name of the procedure it didn't modify. Prior to running the code please add the reference "Microsoft Visual Basic for Applications Extensibility 5.3" to your project. &lt;br /&gt;&lt;br /&gt;Note: The form specified in strForm below must be in design view prior to running the code in order for this to work. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can call the procedure using:&lt;br /&gt;AddCodeToControls("Form_myForm", "Msgbox " &amp; chr(34) &amp; "Hello World" &amp; chr(34) , "BeforeUpdate"&lt;br /&gt;&lt;br /&gt;&lt;Code&gt;&lt;br /&gt;' This code was originally written by Juan Soto at AccessExperts.net.&lt;br /&gt;' It is not to be altered or distributed,&lt;br /&gt;' except as part of an application.&lt;br /&gt;' You are free to use it in any application,&lt;br /&gt;' provided the copyright notice is left unchanged.&lt;br /&gt;'&lt;br /&gt;' Code Courtesy of&lt;br /&gt;' Juan Soto at AccessExperts.net&lt;br /&gt;&lt;br /&gt;Public Function AddCodeToControls(strFormName As String, strCode As String, strProcedure As String)&lt;br /&gt;    On Error Resume Next&lt;br /&gt;    Dim VBAEditor As VBIDE.VBE&lt;br /&gt;    Dim VBProj As VBIDE.VBProject&lt;br /&gt;    Dim VBComp As VBIDE.VBComponent&lt;br /&gt;    Dim CodeMod As VBIDE.CodeModule&lt;br /&gt;    Dim obj As Object&lt;br /&gt;    Dim frm As Form&lt;br /&gt;    Dim ctl As Access.Control&lt;br /&gt;    Dim lngHeaderLine As Long&lt;br /&gt;    &lt;br /&gt;    Set VBAEditor = Application.VBE&lt;br /&gt;    Set VBProj = VBAEditor.ActiveVBProject&lt;br /&gt;    Set VBComp = VBProj.VBComponents(strFormName)&lt;br /&gt;    Set CodeMod = VBComp.CodeModule&lt;br /&gt;    Set frm = Forms(strform)&lt;br /&gt;    For Each ctl In frm.Controls&lt;br /&gt;        If ctl.ControlType = acCheckBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox _&lt;br /&gt;            Or ctl.ControlType = acTextBox Then&lt;br /&gt;            'Search if object already has an after update event&lt;br /&gt;            lngHeaderLine = CodeMod.ProcStartLine(ctl.Name &amp; "_" &amp; strProcedure, vbext_pk_Proc)&lt;br /&gt;            If Err &gt; 0 Then&lt;br /&gt;                'Procedure does not exist, create it&lt;br /&gt;                lngHeaderLine = CodeMod.CreateEventProc(strProcedure, ctl.Name)&lt;br /&gt;                CodeMod.InsertLines lngHeaderLine + 1, strCode&lt;br /&gt;            Else&lt;br /&gt;                'Procedure does exist, print name for manual editing later&lt;br /&gt;                Debug.Print ctl.Name &amp; "_" &amp; strProcedure &amp; " Not Modified"&lt;br /&gt;            End If&lt;br /&gt;        End If&lt;br /&gt;    Next ctl&lt;br /&gt;&lt;br /&gt;End Function&lt;br /&gt;&lt;/Code&gt;</description>
      <pubDate>Mon, 28 Apr 2008 01:38:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5436</guid>
      <author>AccessExpert (Juan Soto)</author>
    </item>
  </channel>
</rss>
