{"id":397,"date":"2016-07-12T19:21:43","date_gmt":"2016-07-12T23:21:43","guid":{"rendered":"http:\/\/access-diva.com\/blog\/?p=397"},"modified":"2016-07-13T02:51:34","modified_gmt":"2016-07-13T06:51:34","slug":"appointment-collision-check","status":"publish","type":"post","link":"https:\/\/access-diva.com\/blog\/?p=397","title":{"rendered":"Appointment Collision Check"},"content":{"rendered":"<p>You&#8217;ve created a database for scheduling appointments, one little problem when entering these appointments you can&#8217;t tell if it clashes with another appointment (especially, if you are not the only one entering).\u00a0 You could use a <a href=\"http:\/\/allenbrowne.com\/appevent.html\" target=\"_blank\">query<\/a> but I always thought this would be handled better in a Module besides, I had an old database from 2002 <span style=\"color: #999999;\">(sorry, site no longer up so I can&#8217;t post a link)<\/span> that was already part of the way there.\u00a0 So, this looked like a good time to *make it work*!\u00a0 Let&#8217;s get started&#8230;<\/p>\n<h5>My parameters:<\/h5>\n<p><em><span style=\"color: #999999;\">(In this example I am using the below table, you will need to change the names here and in the Module to match yours.)<\/span><\/em><\/p>\n<p>tblActivities<br \/>\naActivityID (PK, Autonumber)<br \/>\naAssociateID (FK related to tblAssociateProfile, Number, Long Integer)<br \/>\naDate (Date\/Time, Short Date [ No need to use Input Mask at Table Level ])<br \/>\naStartTime (Date\/Time, Medium Time [ Input Mask: <strong>99:00\\ &gt;LL;0;_ <\/strong>])<br \/>\naEndTime (Date\/Time, Medium Time [ Input Mask: <strong>99:00\\ &gt;LL;0;_ <\/strong>])<br \/>\netc&#8230;<\/p>\n<h6>Module (Named: modCollisionCheck)&#8230;<\/h6>\n<p>You can incorporate into an already existing Module but for ease\u00a0you can copy\/paste the below into a new Module.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Option Compare Database\r\nOption Explicit\r\n\r\nPublic pubActivityID As Long\r\n\r\nPublic Function getActivityID()\r\n    getActivityID = pubActivityID\r\nEnd Function\r\n\r\n\r\nFunction fCollisionCheck(dteDate As Date, stStartTime As Date, etEndTime As Date, lngAssociateID As Long)\r\n'7.11.2016 rtw modified\r\n'http:\/\/www.access-diva.com\/blog\/p=397\r\n    \r\n    Dim dbDuration As Double\r\n    Dim se As Date 'Starting time of appointments already made (in table)\r\n    Dim ee As Date 'Ending time of appointments already made (in table)\r\n    Dim blCollision As Boolean\r\n    Dim db As DAO.Database\r\n    Dim rs As DAO.Recordset\r\n    Dim frm As Form\r\n    \r\n    Set frm = Screen.ActiveForm\r\n    Set db = CurrentDb\r\n    Set rs = db.OpenRecordset(&quot;tblActivities&quot;, dbOpenDynaset)\r\n    \r\n    blCollision = False\r\n    frm.lblCollisionCheck.Caption = &quot; &quot;\r\n    dbDuration = etEndTime - stStartTime\r\n    \r\n        With rs\r\n          Do Until rs.EOF\r\n          'First check if the appointment date on the form is the same as the record in the table.\r\n          'The recordset could be filtered at this point thus avoiding\r\n          'to have to loop through all the records in the Appointment table.\r\n          If !aDate = dteDate And !aAssociateID = lngAssociateID Then\r\n              se = !aStartTime\r\n              ee = !aStartTime + dbDuration\r\n            'Lembit Soobik's nifty formula for checking collisions\r\n            If stStartTime &lt; ee And etEndTime &gt; se And pubActivityID &lt;&gt; !aActivityID Then\r\n                blCollision = True\r\n                frm.lblCollisionCheck.Caption = &quot;Collision&quot;\r\n                'MsgBox &quot;Collision with Appointment ID: &quot; &amp; !aActivityID &amp; vbCrLf _\r\n                &amp; &quot;Starting at: &quot; &amp; se &amp; vbCrLf _\r\n                &amp; &quot;Ending at:  &quot; &amp; ee, vbExclamation, &quot;Collision Check&quot;\r\n            End If\r\n          End If\r\n            .MoveNext\r\n          Loop\r\n            rs.Close\r\n            Set rs = Nothing\r\n        End With\r\n    \r\n    db.Close\r\n    Set db = Nothing\r\n        \r\n        If Not blCollision Then\r\n          frm.lblCollisionCheck.Caption = &quot; &quot;\r\n          'MsgBox &quot;No collisions encountered&quot;, vbInformation, &quot;You made it&quot;\r\n        End If\r\n\r\nEnd Function\r\n<\/pre>\n<h6>Form&#8230;<\/h6>\n<p>Place Label, <strong>lblCollisionCheck<\/strong>,\u00a0on your Form, in this example it&#8217;s above the Command Buttons.\u00a0 You will <strong>need<\/strong> to place one\u00a0on your Form\u00a0use that potion of the code.\u00a0 However, if you like you can comment out those lines and use the lines below that are pop-up messages.\u00a0 (I personally like the Label as no extra clicking required by the End User.)<\/p>\n<p>To use the Module above the Form must be set to <strong>Pop-Up = No<\/strong>. If you want the Form to be a Pop-Up in the Module above you must change this&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Set frm = Screen.ActiveForm<\/pre>\n<p>to<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Set frm = Forms!&#x5B;YOUR FORM NAME]<\/pre>\n<p><strong>Side Note<\/strong>: If you want to use a Main Form\\Subform set up then the <strong>lblCollisionCheck<\/strong> must be on the Main Form, all other Controls can be on the Subform.\u00a0 Since we are reading the Recordsource of the Subform no need to *worry about* the other Controls.<\/p>\n<p>In the <strong>After_Update<\/strong> event of <strong>txtStartTime<\/strong> place (note the name of the Controls):<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">    If Not IsNull(Me.txtEndTime) Then\r\n            Call fCollisionCheck(Me.txtDate, Me.txtStartTime, Me.txtEndTime, Me.cboAssociateID)\r\n    End If\r\n<\/pre>\n<p>In the <strong>After_Update<\/strong> event of <strong>txtEndTime<\/strong> place (note the name of the Controls):<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">    pubActivityID = Me.txtActivityID\r\n    \r\n    Call fCollisionCheck(Me.txtDate, Me.txtStartTime, Me.txtEndTime, Me.cboAssociateID)\r\n<\/pre>\n<p>In the <strong>Lost_Focus<\/strong> event of both the <strong>txtStartTime<\/strong> and <strong>txtEndTime<\/strong> place (I like the extra Save.):<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">DoCmd.RunCommand acCmdSaveRecord<\/pre>\n<p>In the Forms <strong>On_Close<\/strong> event (We want to remember to drop the Public Variable when closing the Form\u00a0in case we want to use it\u00a0elsewhere.):<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">pubActivityID = Empty<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-399\" src=\"http:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsMenu.png\" alt=\"Appointments\" width=\"659\" height=\"632\" srcset=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsMenu.png 659w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsMenu-300x288.png 300w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsMenu-150x144.png 150w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsMenu-400x384.png 400w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/p>\n<p>So, if you enter an appointment that conflicts with another one (See the message?)&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-400\" src=\"http:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsCollision.png\" alt=\"appointmentsCollision\" width=\"659\" height=\"632\" srcset=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsCollision.png 659w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsCollision-300x288.png 300w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsCollision-150x144.png 150w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2016\/07\/appointmentsCollision-400x384.png 400w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><br \/>\nNow, just change the time and message, Collision, will go away provided there still isn&#8217;t one!<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_397\" class=\"pvc_stats all  \" data-element-id=\"397\" 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:\/\/access-diva.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>You&#8217;ve created a database for scheduling appointments, one little problem when entering these appointments you can&#8217;t tell if it clashes with another appointment (especially, if you are not the only one entering). You could use a query but I always thought this would be handled better in a Module besides, I had an old database [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_397\" class=\"pvc_stats all  \" data-element-id=\"397\" 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:\/\/access-diva.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,71],"tags":[93,20,76],"class_list":["post-397","post","type-post","status-publish","format-standard","hentry","category-access-tips","category-forms","tag-collision-check","tag-forms","tag-vba","odd"],"_links":{"self":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=397"}],"version-history":[{"count":15,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}