Autonumber

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jahangir
    New Member
    • Oct 2006
    • 22

    #1

    Autonumber

    When i delete some records in tabel in which primary key field is set for autonumber, and enter again in this tabel the autonumber starts from deleted numbers.

    how i can set the autonumber start from 1.?
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by Jahangir
    When i delete some records in tabel in which primary key field is set for autonumber, and enter again in this tabel the autonumber starts from deleted numbers.

    how i can set the autonumber start from 1.?
    The simple answer is you can't.

    The autonumber is set by Access and it doesn't reset to 1 just because you deleted the records. Running compact and repair facility in the Database Utilities section of the Tools menu usually resets this. However, this is not a practical solution and by no means guaranteed to work.

    Usually if you need a specified numbering system you wouldn't use the autonumber data type but set the number to increment programmaticall y using vba behind the data entry form that populates this table. If you want to know how to do this let me know.

    Comment

    • pks00
      Recognized Expert Contributor
      • Oct 2006
      • 280

      #3
      to add further

      If u deleted ALL records then did a compact/repair, that should restart the numbering. Ive yet to see otherwise, but if u still have records there, it does not restart from the last record

      Comment

      • PEB
        Recognized Expert Top Contributor
        • Aug 2006
        • 1418

        #4
        There is also a way to restart the numbering...

        Using Append or better Update query on the respective Autonumber...


        Using this maner I control my autonumbering.. ..

        Be....en, hein...., yeah.... good...., sometimes it arrives that it generates a number that exists, but it's my fault ;)

        :)

        Comment

        • hyperpau
          Recognized Expert New Member
          • Jun 2007
          • 184

          #5
          Originally posted by mmccarthy
          The simple answer is you can't.

          The autonumber is set by Access and it doesn't reset to 1 just because you deleted the records. Running compact and repair facility in the Database Utilities section of the Tools menu usually resets this. However, this is not a practical solution and by no means guaranteed to work.

          Usually if you need a specified numbering system you wouldn't use the autonumber data type but set the number to increment programmaticall y using vba behind the data entry form that populates this table. If you want to know how to do this let me know.

          I want to learn how to do it. please let me know.

          I have a table named tblReceive with a field [ReceiveNo] having the format "HPRec 07"00
          Then I have a form named frmReceive with a textbox bound to
          tblReceive![ReceiveNo]

          I want the form to generate the ReceiveNo incremented by 1 everytim
          a user adds a new record. I tried this code:

          Code:
          Private Sub Form_Load()
          
          If Me.NewRecord Then
          
          Me![ReceiveNo] = DMax([ReceiveNo], "tblReceive") + 1
          
          Else
          Me![ReceiveNo] = Me![ReceiveNo]
          
          End If
          
          End Sub
          But it doesn't work. as soon as I opne the fomr in Add Entry mode
          it always just shows the [ReceiveNo] Field as HPRec 0700.

          It just does not add 1 to it. can you help me please?

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            The simple fact is that Autonumber is not intended to be used/manipulated by either the developer or the end user! Its meant to be used by Access for housekeeping (behind the scenes) operations! If you need a number to use for manipulating your records, you should follow Mary's advice above.

            Comment

            • nyx
              New Member
              • Feb 2007
              • 7

              #7
              Originally posted by mmccarthy
              The simple answer is you can't.

              The autonumber is set by Access and it doesn't reset to 1 just because you deleted the records. Running compact and repair facility in the Database Utilities section of the Tools menu usually resets this. However, this is not a practical solution and by no means guaranteed to work.

              Usually if you need a specified numbering system you wouldn't use the autonumber data type but set the number to increment programmaticall y using vba behind the data entry form that populates this table. If you want to know how to do this let me know.
              How do you do this? I'm stuck on a similar problem and I think this would help.

              Comment

              • hyperpau
                Recognized Expert New Member
                • Jun 2007
                • 184

                #8
                Customized Autonumber

                As for all you who may want to know this as well, look at the next post.
                Last edited by hyperpau; Aug 20 '07, 12:51 AM. Reason: accidentally submitted.

                Comment

                • hyperpau
                  Recognized Expert New Member
                  • Jun 2007
                  • 184

                  #9
                  Originally posted by nyx
                  How do you do this? I'm stuck on a similar problem and I think this would help.
                  I finally learned how to do it.


                  nyx, heads up.


                  I wanted to have a customized autonumber.
                  This is a receiving number for all items received in a warehouse.
                  The format I wanted would be HPREC 070800001
                  Where in HPREC is a standard prefix for all received numbers.
                  0708 represents the current year and the current month which automatically changes everymonth (0709, 0710, etc)
                  and the 00001 is the number that automatically increments by one for every new record of a received item.
                  (this autonumber assumes that there is a maximum of 99,999 items received every month.)


                  To do this, I removed the autonumber field in my table and I changed it with a text field. I made that field the index key.


                  Then havning that table as the source of my form, i put this code on the on current event of the form.



                  [CODE=vb]Private Sub Form_Current()

                  If Me.NewRecord Then
                  Dim NextNumber As Long
                  NextNumber = Nz(DMax("Right([ReceiveNo], 5)", "tblReceive "), 0) + 1

                  Me![ReceiveNo] = "HPREC " & CStr(Format(Dat e, "yymm")) & Format(CStr(Nex tNumber), "00000")

                  Else
                  Me![ReceiveNo] = Me![ReceiveNo]
                  End If

                  End Sub[/CODE]

                  How does it work?
                  First, the DMax() tells acces to find the largest number in the [ReceiveNo] field of the tblReceive table.
                  After finding it, the Right([ReceiveNo], 5) tells access to strip that
                  largest number of any characters from the left and only look on the last 5 characters of that largest number in the [ReceiveNo] field.
                  (ex: the largest number in my field is HPREC 070800001, then acces only looks at the 00001 part)

                  After locating the last 5 characters of the largest number, the + 1 adds one to it.

                  However, this is not possible if there are no entries in the table yet, meaning there is nothing to looks at the last 5 characters. That is why then the Nz() tells access that the largest number is 0. Therefore, 0 + 1 = 1 which is gonna be the new entry number.

                  then access assings that to my variable named NextNumber.

                  After finding that nextnumber, access now assigns this to the [ReceiveNo] of the form.

                  "HPREC " tells access to put HPREC and a space in front of the nubmers.
                  Format(Date, "yymm") tells access to look at the current year and month based on the computer settings and format it as yymm (0708 if it is august 2007). Then CStr() converts that into a string or text.

                  finally, Format(NextNumb er, "00000") tells access to see the value of the NextNumber variable above and no matter what number that is, format it as a 5 digit number. (so if it is 1, then it would be 00001 and so on)
                  CStr() converts that to a string again.

                  the & sign puts them all together.
                  HPREC + 0708 + 00001

                  and there you go, your own customized autonumber.


                  Disadvantages:
                  1. Since this coding is made on the form, the autoassigning of the Receive Number will not work if you are inputting data directly into the table.

                  2. If the user clicks the UNDO button a lot of times, then the autoassignment of the ReceiveNo will be undone as well, therefore if the user accidentally undoes it and then makes entry again and save it, access shows an error saying that the primary key cannot be null.

                  However, you can fix that by doing an encoding on the save or close button of the form.


                  [CODE=vb]Private Sub cmdSave_Click()

                  If IsNull(Me![ReceiveNo]) Then
                  Dim NextNumber As Long
                  NextNumber = Nz(DMax("Right([ReceiveNo], 5)", "tblReceive "), 0) + 1

                  Me![ReceiveNo] = "HPREC " & CStr(Format(Dat e, "yymm")) & Format (CStr(NextNumbe r), "00000")
                  DoCmd.RunComman d acCmdSaveRecord


                  Else
                  DoCmd.RunComman d acCmdSaveRecord
                  End If

                  End Sub[/CODE]


                  This just simply tells access that if the Save button is clicked, check first if the ReceiveNo field is empty, if it is, then autoassign the incremented ReceiveNo first and then save it. If it is not empty, then just save it right away.


                  I hope this helps you. :)

                  Comment

                  • nyx
                    New Member
                    • Feb 2007
                    • 7

                    #10
                    Wow thanks so much... going to try it right now.

                    Comment

                    • nyx
                      New Member
                      • Feb 2007
                      • 7

                      #11
                      How can I have it generate an autonumber if I want to restart the numbering each year?

                      Comment

                      • missinglinq
                        Recognized Expert Specialist
                        • Nov 2006
                        • 3533

                        #12
                        Another disadvantage with this type of scheme, if used in a multi-user environment, is that two users could retrieve the same value with DMax() and thus you could have two records using the same ReceiveNo.

                        And please, stop calling these Autonumbers! Call them incrementing ID numbers or whatever else you like, but don't call them Autonumbers! Autonumber is an Access datatype; it is a long Integer, can only be generated by Access and cannot be manipulated by the user. Calling anything else an "autonumber " is being needlessly confusing.

                        Linq ;0)>

                        Comment

                        • imrosie
                          New Member
                          • May 2007
                          • 222

                          #13
                          Originally posted by mmccarthy
                          The simple answer is you can't.

                          The autonumber is set by Access and it doesn't reset to 1 just because you deleted the records. Running compact and repair facility in the Database Utilities section of the Tools menu usually resets this. However, this is not a practical solution and by no means guaranteed to work.

                          Usually if you need a specified numbering system you wouldn't use the autonumber data type but set the number to increment programmaticall y using vba behind the data entry form that populates this table. If you want to know how to do this let me know.
                          Hi MMCCarthy

                          I would love to know how to do this...I really don't like using autonumbers, they've given me so much trouble, but I need a way to assign account and order number (I'm a newbie),,,,so if you have an example please share.
                          thanks
                          Rosie

                          Comment

                          • hyperpau
                            Recognized Expert New Member
                            • Jun 2007
                            • 184

                            #14
                            Originally posted by missinglinq
                            Another disadvantage with this type of scheme, if used in a multi-user environment, is that two users could retrieve the same value with DMax() and thus you could have two records using the same ReceiveNo.

                            And please, stop calling these Autonumbers! Call them incrementing ID numbers or whatever else you like, but don't call them Autonumbers! Autonumber is an Access datatype; it is a long Integer, can only be generated by Access and cannot be manipulated by the user. Calling anything else an "autonumber " is being needlessly confusing.

                            Linq ;0)>
                            that's right!!! i agree :)

                            Comment

                            • hyperpau
                              Recognized Expert New Member
                              • Jun 2007
                              • 184

                              #15
                              Originally posted by nyx
                              How can I have it generate an autonumber if I want to restart the numbering each year?

                              The example i have given you restarts the number every month, and everyyear. :)
                              well, actually I am not sure. but I would assume so.

                              Missingling, could you give us some light to this ?

                              Comment

                              Working...