{"id":1058,"date":"2015-12-22T14:57:43","date_gmt":"2015-12-22T19:57:43","guid":{"rendered":"http:\/\/regina-whipp.com\/blog\/?p=1058"},"modified":"2016-01-03T18:10:45","modified_gmt":"2016-01-03T23:10:45","slug":"eventprocedures","status":"publish","type":"post","link":"https:\/\/regina-whipp.com\/blog\/?p=1058","title":{"rendered":"Call Me&#8230;"},"content":{"rendered":"<h6>Calling an Event Procedure\u00a0from one Form to another&#8230;<\/h6>\n<p>Every once in a while I need to use the same code I have in the Event Procedure of a one Control in another Forms Event Procedure, whether it be the On_Click event of a Command Button or the After_Update of a Combo Box.\u00a0 99% of the time I would just turn that into in Function or a Sub and drop it into a Module and done, I can now reuse it over and over with no issues.\u00a0 So why would I or anyone need to do this?\u00a0 Simple\u2026 the Users makes an update on a Subform and I need to refresh the Main Form and that code is specific to the Controls on that Form\/Subform, what easier way than to just reuse the code already available in an Event Procedure in another Form.\u00a0 (This happens about 1% of the time, if not less.\u00a0 It is not something I would recommend to do regularly.)\u00a0 Now, I could just copy\/paste it to the Subform but if I ever have to make a change I have to remember to hit both places!\u00a0 Leaving it where it is makes sense, now, I just need to change it in one place.<\/p>\n<p>So now, the specifics\u2026<\/p>\n<p>First and foremost, you must either remove the <strong>Private<\/strong> from in front of the Sub and\/or change it to <strong>Public<\/strong> but it\u2019s not necessary.\u00a0 I tend to change it to Public, only because if I need to find it I can use <strong>Find<\/strong> &gt; <strong>Public Sub<\/strong> as a parameter finding it quickly.\u00a0 (Using <strong>Find<\/strong> &gt; <strong>Sub<\/strong> will cause me to stop at every Event in every Module!)<\/p>\n<p>Example\u2026<br \/>\nWas<strong> Private<\/strong> Sub cboSearch_AfterUpdate changed to <strong>Public<\/strong> Sub cboSearch_AfterUpdate()<\/p>\n<p style=\"padding-left: 30px;\">And the difference between Public Sub and Private Sub?\u00a0 Ken Sheridan explains it best&#8230;<\/p>\n<blockquote><p>Declaring a function in a standard module Public exposes it throughout the database, so it can be called from queries, forms, code in other modules, etc.\u00a0 Declaring a function in a standard module Private exposes it only within the module, so should be done where a function is called by another function or procedure in the same module, but not by anything outside the module.\u00a0\u00a0 The same name can be used for Private functions in different modules, but a Public function&#8217;s name must be distinct within the whole database.<\/p><\/blockquote>\n<p style=\"padding-left: 30px;\">From: <a href=\"http:\/\/answers.microsoft.com\/en-us\/office\/forum\/office_2010-access\/private-or-public-functions\/e811ca47-ae9e-45e0-84ba-cc9d0384d19c\" target=\"_blank\">Private or Pubic Functions?<\/a><\/p>\n<p>Then you will need to make sure the Form that you are referencing is open.\u00a0 That said, you can open it minimized or hidden but it must be open or you cannot access the Public Function.<\/p>\n<p><strong>From Subform (Child) to Main Form (Parent)<\/strong><br \/>\nFunction or Sub<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall Forms.frmYourForm.pubYourPublicFunction\r\n<\/pre>\n<p>OR\u2026<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall Forms(&quot;frmYourForm&quot;).pubYourPublicFunction\r\n<\/pre>\n<p>Combo Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall Forms(&quot;frmYourForm&quot;).cmdYourComboBox_AfterUpdate\r\n<\/pre>\n<p>OR&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.Parent.cmdYourComboBox_AfterUpdate\r\n<\/pre>\n<p>Command Button<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nForms(&quot;frmYourForm&quot;).cmdYourCommandButton_Click\r\n<\/pre>\n<p>OR&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.Parent.cmdYourCommandButton_Click\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>From Main Form (Parent) to Subform (Child) Form<\/strong><br \/>\nFunction or Sub<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.sfrYourSubform.Form.pubYourPublicFunction\r\n<\/pre>\n<p>Combo Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.sfrYourSubform.Form.cmdYourComboBox_AfterUpdate\r\n<\/pre>\n<p>Command Button<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.sfrYourSubform.Form.cmdYourCommanButton_Click\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>From another Control on the same Form (Parent or Child)<\/strong><br \/>\nFunction or Sub (The same way you would call a normal Module)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall pubYourPublicFunction\r\n<\/pre>\n<p>Combo Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall cboYourComboBox_AfterUpdate\r\n<\/pre>\n<p>Command Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCall cmdYourCommanButton_Click\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>From Subform (Child) to Main Form (Parent) of Main Form (Parent) Form<\/strong><br \/>\nFunction or Sub<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.Parent.Parent.pubYourPublicFunction\r\n<\/pre>\n<p>Combo Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.Parent.Parent.cmdYourComboBox_AfterUpdate\r\n<\/pre>\n<p>Command Button<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.Parent.Parent.cmdYourCommandButton_Click\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>From a Public Module to Subform (Child) via Main Form (Parent)<\/strong><br \/>\nFunction or Sub<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nForms(&quot;frmMain&quot;)(&quot;sfrYourSubform&quot;).pubYourPublicFunction\r\n<\/pre>\n<p>Combo Box<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nForms(&quot;frmMain&quot;)(&quot;sfrYourSubform&quot;).cmdYourComboBox_AfterUpdate\r\n<\/pre>\n<p>Command Button<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nForms(&quot;frmMain&quot;)(&quot;sfrYourSubform&quot;).cmdYourCommandButton_Click\r\n<\/pre>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1058\" class=\"pvc_stats all  \" data-element-id=\"1058\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Calling an Event Procedure from one Form to another&#8230; <\/p>\n<p>Every once in a while I need to use the same code I have in the Event Procedure of a one Control in another Forms Event Procedure, whether it be the On_Click event of a Command Button or the After_Update of a Combo Box. 99% of [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1058\" class=\"pvc_stats all  \" data-element-id=\"1058\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/regina-whipp.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,63],"tags":[64,74,31],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-access-tips","category-database-design","tag-access-tips","tag-calling-event-procedures","tag-forms","odd"],"_links":{"self":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1058"}],"version-history":[{"count":11,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":1182,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions\/1182"}],"wp:attachment":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}