{"id":100,"date":"2012-10-24T01:21:11","date_gmt":"2012-10-24T01:21:11","guid":{"rendered":"http:\/\/regina-whipp.com\/blog\/?p=100"},"modified":"2016-01-01T14:39:00","modified_gmt":"2016-01-01T19:39:00","slug":"count-how-many-times-a-value-appears-in-a-table","status":"publish","type":"post","link":"https:\/\/regina-whipp.com\/blog\/?p=100","title":{"rendered":"Count how many times a value appears in a table&#8230;"},"content":{"rendered":"<p>Ever want to&#8230;<\/p>\n<ul>\n<li>Track how many times the Item has been returned for repairs<\/li>\n<li>Make sure the Item on the Packing Slip has a valid Return Authorization<\/li>\n<li>Make that Spare Part hasn\u2019t already been used for another repair<\/li>\n<\/ul>\n<p>For those times I use&#8230;<\/p>\n<p>Place the below code in a Module remembering not to name the Module the same as the Function.\u00a0 Mine is placed in modUtilities.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nFunction IDOccurs(strTable As String, lngID As Long) As Integer\r\nOn Error GoTo ErrHandler\r\n'From http:\/\/www.access-diva.com\/vba12.html\r\n'For numeric values&lt;\/em&gt;&lt;\/span&gt;\r\n\r\nDim rst As DAO.Recordset\r\nDim db As DAO.Database\r\nDim myQuery As String\r\n\r\n     myQuery = &quot;SELECT * FROM &quot; &amp; strTable &amp; &quot; WHERE lngMyID = &quot; &amp; lngID &amp; &quot;;&quot;\r\n\r\n     Set db = CurrentDb()\r\n     Set rst = db.OpenRecordset(myQuery, dbOpenDynaset, dbSeeChanges)\r\n\r\n     rst.MoveFirst\r\n     rst.MoveLast\r\n\r\n     IDOccurs = rst.RecordCount\r\n\r\nComplete:\r\n   Set rst = Nothing\r\n   db.Close\r\n   Set db = Nothing\r\n   Exit Function\r\n\r\nErrHandler:\r\n   MsgBox (&quot;Error:&quot; &amp; Err.Description)\r\n   IDOccurs = True\r\n   Resume Complete\r\nEnd Function\r\n<\/pre>\n<p><strong><em>And here\u2019s an example of its use&#8230;\u00a0 Note: UpdateUser() is shown below.<\/em><\/strong><\/p>\n<p>Private Sub <a href=\"#UpdateUser\">UpdateUser<\/a>\u00a0(strField As String)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim strID As String\r\nDim lngRecordIDs As Long\r\n\r\nIf strField = &quot;rID&quot; Then\r\n     Me.txtID.SetFocus\r\n     strID = Me.txtID\r\n     lngRecordIDs = IDOccurs(&quot;tblReceiving&quot;, CLng(strID))\r\nElse\r\n     lngRecordIDs = IDOccurrences(&quot;tblReceiving&quot;, &quot;SerialNumber&quot;, strID)\r\nEnd If\r\n\r\n   Select Case lngRecordIDs\r\n     Case 0\r\n      lblUpdate.Caption = &quot;Item has not been received.&quot;\r\n      lblUpdate.ForeColor = vbRed\r\n     Case 1\r\n      lblUpdate.Caption = &quot;Item has been received 1 time.&quot;\r\n      lblUpdate.ForeColor = vbBlack\r\n     Case Else\r\n      lblUpdate.Caption = &quot;Item has been received &quot; &amp; Trim(Str(lngRecordIDs)) &amp; &quot; times!&quot;\r\n      lblUpdate.ForeColor = vbRed\r\n   End Select\r\n<\/pre>\n<p>End Sub<\/p>\n<p>Place the below code in a Module remembering not to name the Module the same as the Function.\u00a0 Mine is placed in modUtilities.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nFunction IDOccurrences(strTable As String, strField, strID As String) As Integer\r\nOn Error GoTo ErrHandler\r\n'From http:\/\/www.access-diva.com\/vba12.html\r\n'For string values\r\n\r\nDim rst As DAO.Recordset\r\nDim db As DAO.Database\r\nDim myQuery As String\r\n\r\n     myQuery = &quot;SELECT * FROM &quot; &amp; strTable &amp; &quot; WHERE &quot; &amp; strField &amp; &quot; = '&quot; &amp; strID &amp; &quot;';&quot;\r\n\r\n     Set db = CurrentDb()\r\n     Set rst = db.OpenRecordset(myQuery, dbOpenDynaset, dbSeeChanges)\r\n\r\n     If rst.RecordCount = 0 Then\r\n        rst.MoveLast\r\n        rst.MoveFirst\r\n     End If\r\n\r\n     IDOccurrences = rst.RecordCount\r\n\r\nComplete:\r\n   Set rst = Nothing\r\n   db.Close\r\n   Set db = Nothing\r\nExit Function\r\n\r\nErrHandler:\r\n   MsgBox (&quot;No occurences with that ID found!&quot;)\r\n   IDOccurrences&gt; = True\r\n   Resume Complete\r\nEnd Function\r\n<\/pre>\n<p><strong><em>And here\u2019s an example of its use&#8230;\u00a0 Notice it is the the BeforeUpdate event of Serial Number.\u00a0 This is to capture the event while it&#8217;s being entered and before it&#8217;s commited to the table.\u00a0 Now, we can quickly Undo should the answer to the question be no.<\/em><\/strong><\/p>\n<p>Private Sub txtSerialNumber_BeforeUpdate(Cancel As Integer)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim intResp As Integer\r\n\r\n  If IDOccurrences(&quot;tblReceiving&quot;, &quot;rSerialNumber&quot;, Me.txtSerialNumber) = 1 Then\r\n     Me.txtSerialNumber.ForeColor = RGB(193, 0, 0)\r\n     DoCmd.OpenForm &quot;frmViewUnit&quot;, , , &quot;&#x5B;rSerialNumber]='&quot; &amp; Me!&#x5B;txtSerialNumber] &amp; &quot;'&quot;, , acDialog \r\n\r\n        intResp = MsgBox(&quot;Serial Number&quot; &amp; Me.txtSerialNumber &amp; &quot; already exists in Receiving table! Continue anyway?&quot;, &quot; &amp; _\r\n                                                 vbYesNo + vbExclamation, &quot;Serial Number&quot;)\r\n\r\n      If intResp = vbYes Then\r\n            Me.txtSerialNumber.ForeColor = RGB(0, 0, 0)\r\n       Else\r\n           Cancel = True\r\n           Me.Undo\r\n      End If\r\n   End If\r\n<\/pre>\n<p>End Sub<\/p>\n<p>Here&#8217;s another sample of how I use <strong>UpdateUser<\/strong>() when the\u00a0processing of data is going to take a few mintes.\u00a0 I can then let them know what&#8217;s going on so they don&#8217;t get over anxious with the mouse while waiting for the process to finish&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nPrivate Sub UpdateUser(strMsg As String)\r\n     lblUpdate.Caption = strMsg\r\n     Me.Repaint\r\n     DoEvents\r\nEnd Sub\r\n<\/pre>\n<p><strong>Legend<\/strong>&#8230;<\/p>\n<p><span style=\"color: #0000ff;\"><strong>dbSeeChanges<\/strong> <\/span>only needed if you\u2019re using tables linked to an SQL Server, otherwise you can omit.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_100\" class=\"pvc_stats all  \" data-element-id=\"100\" 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>Ever want to&#8230;<\/p>\n<p> Track how many times the Item has been returned for repairs Make sure the Item on the Packing Slip has a valid Return Authorization Make that Spare Part hasn\u2019t already been used for another repair <\/p>\n<p>For those times I use&#8230;<\/p>\n<p>Place the below code in a Module remembering not to name the [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_100\" class=\"pvc_stats all  \" data-element-id=\"100\" 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":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,63],"tags":[64,21],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-access-tips","category-database-design","tag-access-tips","tag-vba","odd"],"_links":{"self":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/100","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=100"}],"version-history":[{"count":55,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":1123,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions\/1123"}],"wp:attachment":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}