{"id":704,"date":"2014-09-07T00:22:11","date_gmt":"2014-09-07T04:22:11","guid":{"rendered":"http:\/\/regina-whipp.com\/blog\/?p=704"},"modified":"2017-06-28T16:17:43","modified_gmt":"2017-06-28T20:17:43","slug":"autonumber","status":"publish","type":"post","link":"https:\/\/regina-whipp.com\/blog\/?p=704","title":{"rendered":"Another post on Sequential Numbering&#8230;"},"content":{"rendered":"<p>The most often asked question is about customizing Autonumber.<\/p>\n<p>The answer is, don\u2019t!\u00a0 If you want to use a sequential numbering system Autonumber is not the Data Type to use.\u00a0 Even if you want to include Autonumber with another Field to make a composite&#8230; don\u2019t, it\u2019s not always sequential.\u00a0 (More on that later)\u00a0 Leave Autonumber alone and use it for its intended purpose, a unique Primary Key for your databases internal logic that no one, besides you, sees.\u00a0 And here\u2019s why&#8230;<\/p>\n<p>1. It skips<\/p>\n<ul>\n<li>This is a biggie. Once you delete a record or *spend* it (create and then undo) that number is no longer available, it\u2019s gone for good.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-971 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/MissingNumbers.png\" alt=\"MissingNumbers\" width=\"152\" height=\"341\" \/><\/p>\n<p>2. It\u2019s not always sequential<\/p>\n<p style=\"padding-left: 30px;\">Sometimes Access decides to assign a negative number. Autonumber is only guaranteed to be unique not sequential even if you select <strong>Increment<\/strong> under <strong>New Values<\/strong> at the <strong>Table level<\/strong>.<\/p>\n<p>3. While you can set the initial value (The Access Web &#8211;\u00a0<a href=\"http:\/\/access.mvps.org\/access\/tables\/tbl0005.htm\" target=\"_blank\" rel=\"noopener\"><strong>Have Autonumber field start from a value different from 1<\/strong><\/a> or Allen Browne\u2019s Site<a href=\"%20http:\/\/allenbrowne.com\/ser-26.html\" target=\"_blank\" rel=\"noopener\"><strong> Set AutoNumbers to start from<\/strong><\/a>) you cannot otherwise modify the number. The Autonumber Data Type is not editable and there is no way around that via Access. The only exception to this rule is if you upsized your Table to SQL Server in which case you can modify the number.<\/p>\n<p>4. And, it does not always indicate the order in which records are entered, see Item 2.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-975 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/OrderByID.png\" alt=\"OrderByID\" width=\"213\" height=\"223\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/OrderByID.png 213w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/OrderByID-143x150.png 143w\" sizes=\"auto, (max-width: 213px) 100vw, 213px\" \/><\/p>\n<p>So, now what?\u00a0 Instead, create a separate field for your sequential\/custom number\/string, herein referred to as the Sequential ID.\u00a0 A few tips before we get started&#8230;<\/p>\n<ul>\n<li>Using the Nz() function ensures that the first record is handled correctly, i.e. first record of the year or first record in the table or first record in the group.<\/li>\n<li>You will want to <strong>Save<\/strong> your Record immediately after the Sequential ID has been assigned. This becomes especially important in multi-user databases, so that the Sequential ID does not get *snatched* away from the User and\/or your Users may receive the Error 3022, shown below, if two Users try to save at the same time.<\/li>\n<\/ul>\n<blockquote><p>The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.<\/p><\/blockquote>\n<p>You can implement your own Error Handling or Roger Carlson has some simple code to trap the error, see <a href=\"http:\/\/rogersaccessblog.blogspot.com\/2010\/12\/domain-function-example-simulate.html\" target=\"_blank\" rel=\"noopener\"><strong>Domain Function Example: Simulate AutoNumber with DMax()<\/strong><\/a><\/p>\n<ul>\n<li>Have a back-up plan. When you *commit* your Sequential ID to the Table the Record is saved. However, if the User decides they really did not want to create a new Record you\u2019ll want a way to back out that Sequential ID. You will also want a way to handle *spent* Sequential ID\u2019s, i.e. the Record deleted was not the last record so now there is a gap in the sequence. To find those unused numbers you can use\u2026<\/li>\n<\/ul>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSELECT T.lLiftID+1 AS Missing\r\nFROM tblLifts AS T LEFT JOIN tblLifts AS T1 ON T1.lLiftID=T.lLiftID+1\r\nWHERE (((T1.lLiftID) Is Null));\r\n<\/pre>\n<p style=\"padding-left: 30px;\">You will need to decide for yourself what you want to do with this *orphaned* numbers, let them go or allow your Users to use one of them when creating a new Record.<\/p>\n<p>So, let\u2019s create some Sequential ID\u2019s&#8230;<\/p>\n<p style=\"padding-left: 30px;\"><strong>A.<\/strong> Sequential ID number, pseudo Autonumber<br \/>\n<strong>B.<\/strong> Use a Command Button to assign a Sequential ID<br \/>\n<strong>C.<\/strong> Customize your Sequential ID<\/p>\n<p style=\"padding-left: 60px;\">a. Sequential ID with Year prefixing starting fresh each New Year<br \/>\nb. Sequential ID with YYMMDD with an incremental number suffix<br \/>\nc. Sequential ID strings alpha-numeric or numeric alpha<\/p>\n<p style=\"padding-left: 30px;\"><strong>D.<\/strong> Variations of Sequential ID\u2019s<\/p>\n<p>Side note:\u00a0 While some of the methods here allow you to use Sequential ID as a Primary Key, however, it would not be my first choice.\u00a0 That said the method described in A. is at the top of the list for preferred methods should you decide not to use the Autonumber Data Type but implement your own.\u00a0 Remember, just because you can do so does not mean you should do so.\u00a0 Evaluate your schema and decide what works best for you (and the Database) your Users are only going to see what you let them see.<\/p>\n<p>Special note:\u00a0 The sample code provide here does not provide any Error Handling which means you should implement your own.<\/p>\n<div style=\"padding-left: 30px;\"><strong>A. Sequential ID number, pseudo Autonumber<\/strong><\/div>\n<p>With this example you can use the Sequential ID as a Primary Key by putting it in the Before_Insert of the Form, since the Before_Insert event fires when a User moves to a new Record you can increment and then <strong>Save<\/strong> locking your number in place. (Again, just because you can do thing does not mean you should do a thing&#8230; so, again, use this one as the one the User sees and maintain Autonumber with another field as the Primary Key Access uses.)<\/p>\n<p>Private Sub Form_BeforeInsert(Cancel As Integer)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     If Me.NewRecord Then\r\n        Nz(DMax(&quot;YourIDField&quot;,&quot;YourTable&quot;), 0) + 1\r\n        DoCmd.RunCommand acCmdSaveRecord\r\n     End If\r\n<\/pre>\n<p>End Sub<br \/>\n<strong>Note<\/strong>: You can also use the Before_Update<sup>1<\/sup> event of the Form, and while, the Before_Insert event fires before the Before_Update event the Before_Update event is the only event that *writes to disk* upon firing.<\/p>\n<p>Private Sub Form_BeforeUpdate(Cancel As Integer)<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     If Me.NewRecord Then\r\n        Nz(DMax(&quot;YourIDField&quot;,&quot;YourTable&quot;), 0) + 1\r\n        DoCmd.RunCommand acCmdSaveRecord\r\n      End If\r\n<\/pre>\n<p>End Sub<br \/>\n<b><strong>B. Use a Command Button to assign a Sequential Increment<\/strong><\/b><\/p>\n<p>When using this method you\u2019ll want to *push* the User to a new Record and then create your Sequential ID. After all, we don\u2019t them overwriting an existing Sequential ID. You can either do it automatically&#8230;<\/p>\n<p>Private Sub cmdAddNew_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     If Dirty Then Exit Sub\r\n       DoCmd.GoToRecord , , acNewRec   'Force User to a New Record\r\n       Me.txtCoilID = Nz(DMax(&quot;crCoilID&quot;, &quot;tblCoilReceiving&quot;), 0) + 1\r\n       Me.cboCoilID = Me.txtCoilID\r\n<\/pre>\n<p>End Sub<br \/>\n&#8230;or send them a message to go to a New Record if they are not already on one&#8230;<br \/>\nPrivate Sub cmdAddNew_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     If Not IsNull(Me.txtCoilID) Then\r\n       'Tell User to go to a New Record first\r\n       MsgBox &quot;You MUST go to NEW record first!&quot;, vbCritical, &quot;Add New&quot;\r\n     Exit Sub\r\n     End If\r\n\r\n     Me.txtCoilID = Nz(DMax(&quot;crCoilID&quot;, &quot;tblCoilReceiving&quot;), 0) + 1\r\n     Me.cboCoilID = Me.txtCoilID\r\n<\/pre>\n<p>End Sub<\/p>\n<p style=\"padding-left: 30px;\"><b><strong>C. Customize your Sequential ID<\/strong><\/b><\/p>\n<p style=\"padding-left: 60px;\"><b><strong>a. Sequential ID with Year prefixing starting fresh each New Year<\/strong><\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-981 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/GetMyID.png\" alt=\"GetMyID\" width=\"290\" height=\"172\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/GetMyID.png 290w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/GetMyID-150x88.png 150w\" sizes=\"auto, (max-width: 290px) 100vw, 290px\" \/><\/p>\n<p>This example will only increment up to 99999, see <strong>GetMyID<\/strong>!\u00a0 If you plan to use this method you must be careful you will not have more than 99,999 records per year or, if you want to go higher, you can expand on the spaces (placeholders), however, you still need to take note of *how many* you allot when using this example or this might happen&#8230;<\/p>\n<p>1400000<br \/>\n1400001<br \/>\netc&#8230;<br \/>\n1499999<\/p>\n<p>&#8230;go to add a new Receiving ID and you get&#8230;<\/p>\n<p>1400000<\/p>\n<p>&#8230;and an Error Message because at the end it will *start again*! \u00a0So, let&#8217;s avoid code that locks you into a maximum record count unless that is what you mean to and have an end game, like starting a new unique sequence.<\/p>\n<h6>Step 1<\/h6>\n<p>Copy\/paste the below in a Module then Debug &gt; Compile.\u00a0 Be sure to change the name of the Fields and Table below to match your own.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nFunction GetMyID() As Long\r\n   GetMyID = Format(Date, &quot;yy&quot;) &amp; Format(Nz(DMax(&quot;rSequenceID&quot;, &quot;tblReceiving&quot;, &quot;Year(&#x5B;rReceiveDate]) = &quot; &amp; Year(Date)), 0) + 1, &quot;00000&quot;)\r\nEnd Function\r\n<\/pre>\n<h6>\u00a0Step 2<\/h6>\n<p>Create your Command Button and the copy\/paste the lines between Private Sub\u2026 and End Sub in the Event Procedure of your Command Button.\u00a0 There are two procedures listed below, one with validation and one without.\u00a0 You only need to copy\/paste the one you want to use not both.\u00a0 Again, be sure to change the name of the Fields and Table below to match your own.<\/p>\n<p>With validation\u2026<\/p>\n<p style=\"padding-left: 60px;\">Private Sub <strong>cmdReceive<\/strong>_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Inform User they are not on a New Record\r\n     If Not IsNull(Me.txtReceiveID) Then\r\n        MsgBox &quot;You MUST go to NEW record first!&quot;, vbCritical, &quot;Receive&quot;\r\n        Exit Sub\r\n     End If\r\n     \r\n     'Validate that all other required Controls are filled in prior to creating Sequential ID.\r\n     If sfrValidateData Then\r\n        Me.rSequenceID = Nz(DMax(&quot;rSequenceID&quot;, &quot;tblReceiving&quot;, &quot;Year(&#x5B;rReceiveDate]) = &quot; &amp; Year(Date)), 0) + 1\r\n        DoEvents\r\n        Me.txtReceiveID = &amp;lt;strong&amp;gt;GetMyID()\r\n        DoCmd.RunCommand acCmdSaveRecord\r\n        'Requery the Combo Box to exclude the newly received item from the Combo Box which shows items not yet received\r\n        Me.cboResults.Requery\r\n     Else\r\n        MsgBox &quot;Oops, no Receive ID created, please retry!&quot;\r\n     End If\r\n<\/pre>\n<p style=\"padding-left: 60px;\">End Sub<\/p>\n<p><strong>OR<\/strong> if no validation is needed\u2026<\/p>\n<p>Without validation\u2026<\/p>\n<p style=\"padding-left: 60px;\">Private Sub <strong>cmdReceive<\/strong>_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Inform User they are not on a New Record\r\n     If Not IsNull(Me.txtReceiveID) Then\r\n        MsgBox &quot;You MUST go to NEW record first!&quot;, vbCritical, &quot;Receive&quot;\r\n        Exit Sub\r\n     End If\r\n     \r\n        Me.rSequenceID = Nz(DMax(&quot;rSequenceID&quot;, &quot;tblReceiving&quot;, &quot;Year(&#x5B;rReceiveDate]) = &quot; &amp; Year(Date)), 0) + 1\r\n        DoEvents\r\n        Me.txtReceiveID = GetMyID()\r\n        DoCmd.RunCommand acCmdSaveRecord\r\n      \r\n        'Requery the Combo Box to exclude the newly received item\r\n        Me.cboResults.Requery\r\n<\/pre>\n<p style=\"padding-left: 60px;\">\u00a0End Sub<\/p>\n<p><b>\u00a0<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b><strong>b. Sequential ID with YYMMDD with an incremental number suffix<\/strong><\/b>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-984 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/SequentialYYMMDD.png\" alt=\"SequentialYYMMDD\" width=\"324\" height=\"209\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/SequentialYYMMDD.png 324w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/SequentialYYMMDD-300x193.png 300w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/SequentialYYMMDD-150x96.png 150w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><\/p>\n<p>In this example you can easily tell how many Quotes you got on any particular day and still maintain a unique ID.\u00a0 This type of Sequential ID can be useful for tracking sales or sales per Associate\/Employee.\u00a0 You can query the table to check how your business or Sales Staff are doing.\u00a0 One caveat of doing it this way is you will to determine what part is which because you have not formatted two digits for Months and Days.\u00a0 The second example does this and makes it much easier to determine when the Quote was actually created no matter what create date is entered.<\/p>\n<h5>Example One<\/h5>\n<h6>Step 1<\/h6>\n<p>Create a query to count records each day to use in the Function, i.e.<\/p>\n<p style=\"padding-left: 30px;\"><strong>qryCountOfQuotes<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSELECT Left(&#x5B;qQuoteID],InStr(&#x5B;qQuoteID],&quot;-&quot;)-1) AS Quotes, Count(Left(&#x5B;qQuoteID],InStr(&#x5B;qQuoteID],&quot;-&quot;)-1)) AS CountOfQuotes FROM tblQuote GROUP BY Left(&#x5B;qQuoteID],InStr(&#x5B;qQuoteID],&quot;-&quot;)-1);\r\n<\/pre>\n<p>Be sure to change the name of the Fields and Table below to match your own.<\/p>\n<h6>Step 2<\/h6>\n<p>Create your Command Button and the copy\/paste the lines between Private Sub\u2026 and End Sub in the Event Procedure of your Command Button.\u00a0 There are two procedures listed below, one no space holders for double digit Months and Days, Example One and; one with spaces holders for Months and Days, Example Two.\u00a0 The Year is double digit for both examples.\u00a0 You only need to copy\/paste the one you want to use not both.\u00a0 Again, be sure to change the name of the Fields and Table below to match your own.<\/p>\n<p style=\"padding-left: 60px;\">Private Sub<strong> cmdAddNew<\/strong>_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     'Push User to a New Record\r\n     DoCmd.GoToRecord , , acNewRec\r\n     Me.txtQuoteID = Format(Date, &quot;yy&quot;) &amp; Format(Date, &quot;m&quot;) &amp; Format(Date, &quot;d&quot;) &amp; &quot;-&quot; &amp; Nz(DLookup(&quot;CountOfQuotes&quot;, &quot;qryCountOfQuotes&quot;, &quot;Quotes = &quot; &amp; Format(Date, &quot;yy&quot;) &amp; Format(Date, &quot;m&quot;) &amp; Format(Date, &quot;d&quot;)), 0) + 1\r\n     Me.cboQuoteID = Me.txtQuoteID\r\n<\/pre>\n<p style=\"padding-left: 60px;\">\u00a0End Sub<\/p>\n<p><strong>OR<\/strong><\/p>\n<h5>Example Two<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-988 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/qryCountOfQuotes.png\" alt=\"qryCountOfQuotes\" width=\"369\" height=\"227\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/qryCountOfQuotes.png 369w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/qryCountOfQuotes-300x184.png 300w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/qryCountOfQuotes-150x92.png 150w\" sizes=\"auto, (max-width: 369px) 100vw, 369px\" \/><\/h5>\n<p style=\"padding-left: 60px;\">\u00a0Private Sub<strong> cmdAddNew<\/strong>_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     Me.txtQuoteID = Format(Date, &quot;yymmdd&quot;) &amp; &quot;-&quot; &amp; Nz(DLookup(&quot;CountOfQuotes&quot;, &quot;qryCountOfQuotes&quot;, &quot;Quotes = &quot; &amp; Format(Date, &quot;yymmdd&quot;), 0) + 1\r\n<\/pre>\n<p style=\"padding-left: 60px;\">End Sub<\/p>\n<p style=\"padding-left: 30px;\"><b><strong>c. Sequential ID strings alpha-numeric or numeric alpha<\/strong><\/b><\/p>\n<p>Here are two examples using incremental letters; the first will be using an YYDDMM format implementing the letter at the end. The second example I\u2019m using Property Cards which, in this case, only have a letter if there is more than one card. The Property already has a Parcel Number, so no incrementing of the Sequential ID needed.<\/p>\n<h5>Example one<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-990 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/140725A.png\" alt=\"140725A\" width=\"390\" height=\"204\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/140725A.png 390w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/140725A-300x156.png 300w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/140725A-150x78.png 150w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><\/h5>\n<h6>\u00a0Step 1<\/h6>\n<p>Create a query which will be used to create the Sequential ID<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.txtQuoteID = Format(Date,&quot;yymmdd&quot;) &amp; Chr(Nz(DMax(&quot;Asc(Right(qQuoteID,1)),&quot;,&quot;tblQuote&quot;,&quot;Left(qQuoteID,6) = &quot;&quot;&quot; &amp; Format(Date,&quot;yymmdd&quot;) &amp; &quot;&quot;&quot;&quot;),64)+1)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h6>Step 2<\/h6>\n<p>Create your Command Button and the copy\/paste the above line between Private Sub\u2026 and End Sub in the Event Procedure of your Command Button.\u00a0 You only need to copy\/paste the one you want to use.\u00a0 Again, be sure to change the name of the Fields and Table below to match your own.<\/p>\n<p><strong>OR<\/strong><\/p>\n<h5>Example two<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-992 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/123456789B.png\" alt=\"123456789B\" width=\"372\" height=\"188\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/123456789B.png 372w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/123456789B-300x151.png 300w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/123456789B-150x75.png 150w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/p>\n<h6>Step 1<\/h6>\n<p>Create your Command Button and the copy\/paste the lines between Private Sub\u2026 and End Sub in the Event Procedure of your Command Button.\u00a0 You only need to copy\/paste the one you want to use.\u00a0 Again, be sure to change the name of the Fields and Table below to match your own.<\/p>\n<p>Because this is a Property Card and 90% of the information will be duplicated, I am taking what I need from the first card and putting it on the next card.\u00a0 If this is the first time it\u2019s happening, the only card, I first need to add the *A* and then create the new card.<\/p>\n<p style=\"padding-left: 60px;\">Public Sub <strong>cmdDuplicate<\/strong>_Click()<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     Dim intRecCount As Integer\r\n     Dim db As DAO.Database\r\n     \r\n     Set db = DBEngine(0)(0)\r\n     'We need to know how many Property Cards there are to determine if we are starting a *A* or some other letter\r\n     intRecCount = DCount(&quot;ppParcelNumber&quot;, &quot;tblPropertyProfile&quot;, &quot;&#x5B;ppParcelNumber] = '&quot; &amp; Me!&#x5B;txtParcelNumber] &amp; &quot;'&quot;) - 1\r\n\r\n     If Me.Dirty Then\r\n        Me.Dirty = False\r\n     End If\r\n\r\n     If Me.NewRecord Then\r\n        MsgBox &quot;Select an Property Card to duplicate!&quot;, vbInformation, &quot;Duplicate&quot;\r\n     Else\r\n        'If there is only one Property Card assign it an *A*\r\n        If intRecCount = 0 Then\r\n           Me.cboFormID = &quot;A&quot;\r\n        End If\r\n     \r\n     'Duplicate the main record\r\n     With Me.RecordsetClone\r\n        .AddNew\r\n        !ppOwner = Me.txtOwner\r\n        !ppAddress = Me.txtAddress\r\n        !ppLegal = Me.txtLegal\r\n        !ppDTEID = Me.cboDTEID\r\n        !ppPropertyClassificationID = Me.cboPropertyClassificationID\r\n      'Start the lettering from *B* on.\r\n        !ppFormID = Chr(Asc(&quot;B&quot;) + intRecCount)\r\n        !ppParcelNumber = Me.txtParcelNumber\r\n        !ppCountyFIPSID = Me.cboCountyFIPSID\r\n        .Update\r\n        .Bookmark = .LastModified\r\n      Me.Bookmark = .LastModified\r\n      Me.cboParcelNumber = Me.txtPropertyID\r\n      Me.cboParcelNumber.Requery\r\n     End With\r\nEnd If\r\nSet db = Nothing\r\n<\/pre>\n<p style=\"padding-left: 60px;\">End Sub<\/p>\n<p>If you just want to increment letters you can use the below.\u00a0 Note, when using the alphabet you can only go up to *Z* unless you use a custom VBA procedure.\u00a0 See the link below for one such custom procedure.<\/p>\n<p>To Increment Letters&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n&amp;nbsp;&amp;nbsp;&amp;nbsp; If IsNull(Me.txtRevisionID) Then\r\n       Me.txtRevisionID = &quot;A&quot;\r\n    Else\r\n       Me.txtRevisionID = Chr(Asc(Me.txtRevisionID) + 1)\r\n    End If\r\n<\/pre>\n<p>To increment beyond the single letter click <a href=\"http:\/\/www.freevbcode.com\/ShowCode.asp?ID=5440\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p style=\"padding-left: 30px;\"><b><strong>D. Variations<\/strong><\/b><\/p>\n<p><b><strong>I.<br \/>\nT<\/strong><\/b>his method grabs the first three letters of the Company Name. The Company name used in this example is Access Diva. No worries though, if you have two Companies that have the same first three letters it will increment the numeric side to keep the ID unique. This only becomes a problem if you have more than 9,999 Companies that have the identical first three letters.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-995 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/ACC0001.png\" alt=\"ACC0001\" width=\"286\" height=\"220\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/ACC0001.png 286w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/ACC0001-150x115.png 150w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim lngCountIDs As Long\r\n     'Find how many Companies start with the same three letters and count plus one\r\n     lngCountIDs = Nz(DCount(&quot;Right(cCompanyID,3),&quot;, &quot;tblCompanies&quot;, &quot;Left(cCompanyID,3) = '&quot; &amp; Left(Me.txtCompanyName, 3) &amp; &quot;'&quot;), 0) + 1\r\n\r\n     Me.txtCompanyID = StrConv(Trim(Left(Me.txtCompanyName, 3)), vbUpperCase) &amp; Format(lngCountIDs, &quot;0000&quot;)\r\n     DoCmd.RunCommand acCmdSaveRecord\r\n<\/pre>\n<p>You can change how many letters you want use as the Prefix by changing all the 3\u2019s to how ever many letters you want as the Prefix.<\/p>\n<p><strong>II.<br \/>\n<\/strong>Always want it to start with specific letter(s)&#8230;\u00a0 This method starts every Sequential ID with the same letters.\u00a0 Ultimately, you could have a table that determines the Prefix if you want to *tag* which Department, Section or Index Key you assigned the number to.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-996 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/KB.png\" alt=\"KB\" width=\"265\" height=\"175\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/KB.png 265w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/KB-150x99.png 150w\" sizes=\"auto, (max-width: 265px) 100vw, 265px\" \/><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.txtFFESpecification = &quot;KB&quot; &amp; Nz(DMax(&quot;ffeSpecificationID&quot;, &quot;tblFFESpecifications&quot;), 0) + 1\r\n<\/pre>\n<p>&#8230;using a Department, Section or Index Key as a Prefix&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-997 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BR.png\" alt=\"BR\" width=\"294\" height=\"208\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BR.png 294w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BR-150x106.png 150w\" sizes=\"auto, (max-width: 294px) 100vw, 294px\" \/><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.txtFFESpecificationID = Me.cboIndexKeyID.Column(1) &amp; Nz(DMax(&quot;ffeSpecificationID&quot;, &quot;tblFFESpecifications&quot;), 0) + 1\r\n<\/pre>\n<p>&#8230;to add a hyphen\u2026\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-999 size-full\" src=\"http:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BRwithHyphen.png\" alt=\"BRwithHyphen\" width=\"260\" height=\"168\" srcset=\"https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BRwithHyphen.png 260w, https:\/\/regina-whipp.com\/blog\/wp-content\/uploads\/2014\/09\/BRwithHyphen-150x96.png 150w\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" \/><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nMe.txtFFESpecificationID = Me.cboIndexKeyID.Column(1) &amp; &quot;-&quot; &amp; Nz(DMax(&quot;ffeSpecificationID&quot;, &quot;tblFFESpecifications&quot;), 0) + 1\r\n<\/pre>\n<p>To use any of the above create your Command Button and the copy\/paste the lines between Private Sub\u2026 and End Sub in the Event Procedure of your Command Button.\u00a0 You only need to copy\/paste the one you want to use.\u00a0 Again, be sure to change the name of the Fields and Table below to match your own.<\/p>\n<p><strong>III.<br \/>\n<\/strong>Only want to go to a specific number&#8230;<\/p>\n<p style=\"padding-left: 60px;\"><strong>Private Sub Form_BeforeInsert(Cancel as Integer)<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n     Dim iNext As Integer iNext = Nz(DMax(&quot;&#x5B;ID]&quot;, &quot;&#x5B;tablename]&quot;)) + 1 If iNext &amp;gt;= 10000 Then\r\n        Cancel = True\r\n        MsgBox &quot;All ID's have been used, shut off the PC and go home.&quot;, vbOKOnly\r\n     Else Me!&#x5B;ID] = iNext\r\n     End If\r\n<\/pre>\n<p style=\"padding-left: 60px;\">End Sub<\/p>\n<p><strong>IV.<br \/>\n<\/strong>Create a Function for use with more than one table\u2026<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nFunction lngSequentialID(strID As String, strTable As String) As Long\r\n     lngSequentialID = Nz(DMax(strID, strTable), 0) + 1\r\nEnd Function\r\n<\/pre>\n<p>To use copy\/paste the above lines into a Module.\u00a0 To use\u2026<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nlngSequentialID(&quot;YourFieldName&quot;, &quot;YourTableName&quot;)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So, when do those Form Event fire?<\/p>\n<p><strong>Creating a new record<br \/>\n<\/strong>When you move the focus to a new (blank) record on a form and then create a new record by typing in a control, the following sequence of events occurs:<\/p>\n<p style=\"padding-left: 30px;\"><strong>Current<\/strong> (form) \u2192 <strong>Enter<\/strong> (control) \u2192 <strong>GotFocus<\/strong> (control) \u2192 <strong>BeforeInsert<\/strong> (form) \u2192 <strong>AfterInsert<\/strong> (form)<\/p>\n<p>The <strong>BeforeUpdate<\/strong> and <strong>AfterUpdate<\/strong> events for the controls on the form and for the new record occur after the <strong>BeforeInsert<\/strong> event and before the <strong>AfterInsert<\/strong> event.<\/p>\n<p>From:<a href=\"http:\/\/office.microsoft.com\/en-us\/access-help\/order-of-events-for-database-objects-HP005186761.aspx\" target=\"_blank\" rel=\"noopener\"> Order of Events for Database Objects<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>More sites about Sequential Numbering\u2026<br \/>\n<\/strong>Auto Increment a Value <a href=\"http:\/\/www.devhut.net\/2010\/10\/30\/ms-access-auto-increment-a-value\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.devhut.net\/2010\/10\/30\/ms-access-auto-increment-a-value\/<\/a><strong><br \/>\n<\/strong>Sequential Numbering\u00a0<a href=\"http:\/\/scottgem.wordpress.com\/2009\/11\/25\/sequential-numbering\/\">http:\/\/scottgem.wordpress.com\/2009\/11\/25\/sequential-numbering\/<br \/>\n<\/a>Tech on the Net <a href=\"http:\/\/www.techonthenet.com\/access\/modules\/sequential_nbr4.php\">http:\/\/www.techonthenet.com\/access\/modules\/sequential_nbr4.php<br \/>\n<\/a>FMS <a href=\"http:\/\/www.fmsinc.com\/microsoftaccess\/autonumber%20field\/creating.asp\">http:\/\/www.fmsinc.com\/microsoftaccess\/autonumber%20field\/creating.asp<\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_704\" class=\"pvc_stats all  \" data-element-id=\"704\" 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>The most often asked question is about customizing Autonumber.<\/p>\n<p>The answer is, don\u2019t! If you want to use a sequential numbering system Autonumber is not the Data Type to use. Even if you want to include Autonumber with another Field to make a composite&#8230; don\u2019t, it\u2019s not always sequential. (More on that later) Leave Autonumber [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_704\" class=\"pvc_stats all  \" data-element-id=\"704\" 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":[51,57,18,37,56,55,21],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-access-tips","category-database-design","tag-autonumber","tag-custom-autonumber","tag-database-design","tag-dmax","tag-increment","tag-sequential-numbering","tag-vba","odd"],"_links":{"self":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/704","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=704"}],"version-history":[{"count":39,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/704\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/704\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regina-whipp.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}